Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SqlConnection cn = new SqlConnection("Data Source = localhost; Integrated Security = True; Database = myDB;");
- SqlDataAdapter adp = new SqlDataAdapter();
- private void LoadSearch()
- {
- switch (cmbCategory.Text)
- {
- case "All":
- adp.SelectCommand = new SqlCommand("SELECT * FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn);
- DataTable dtAll = new DataTable();
- //cn.Open();
- adp.Fill(dtAll);
- dgCommunications.DataSource = dtAll;
- //cn.Close();
- break;
- case "Incoming Communications":
- adp.SelectCommand = new SqlCommand("SELECT CommType = '" + cmbCategory.Text + "', LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn);
- DataTable dtInc = new DataTable();
- // cn.Open();
- adp.Fill(dtInc);
- dgCommunications.DataSource = dtInc;
- //cn.Close();
- break;
- case "Inside Communications":
- adp.SelectCommand = new SqlCommand("SELECT CommType = '" + cmbCategory.Text + "', LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn); ;
- DataTable dtIns = new DataTable();
- //cn.Open();
- adp.Fill(dtIns);
- dgCommunications.DataSource = dtIns;
- //cn.Close();
- break;
- case "Outgoing Communications":
- adp.SelectCommand = new SqlCommand("SELECT CommType = '" + cmbCategory.Text + "', LetterDate, LetterReceived, LetterType, LetterNumber, LetterAmount, LetterFrom, LetterTo, ReceivedBy, Requisition, LetterSubject, LetterContent, LetterRemarks FROM tblCommunication WHERE LetterType LIKE '" + txSearch.Text.Trim() + "' OR LetterNumber LIKE '" + txSearch.Text.Trim() + "' OR LetterAmount LIKE '" + txSearch.Text.Trim() + "' OR LetterFrom LIKE '" + txSearch.Text.Trim() + "' OR LetterTo LIKE '" + txSearch.Text.Trim() + "' OR ReceivedBy LIKE '" + txSearch.Text.Trim() + "' OR Requisition LIKE '" + txSearch.Text.Trim() + "' OR LetterSubject LIKE '" + txSearch.Text.Trim() + "' OR LetterContent LIKE '" + txSearch.Text.Trim() + "' OR LetterRemarks LIKE '" + txSearch.Text.Trim() + "'", cn); ;
- DataTable dtOut = new DataTable();
- //cn.Open();
- adp.Fill(dtOut);
- dgCommunications.DataSource = dtOut;
- // cn.Close();
- break;
- }
- }
- using (SqlConnection connection = new SqlConnection(conString))
- {
- try
- {
- //your switch case statement
- }
- catch (InvalidOperationException)
- {
- }
- catch (SqlException)
- {
- }
- }
- public void Command(string query,string conString)
- {
- SqlDataAdapter adp = new SqlDataAdapter();
- using (SqlConnection connection = new SqlConnection(conString))
- {
- try
- {
- adp.SelectCommand = new SqlCommand(query,connection);
- DataTable dtOut = new DataTable();
- adp.Fill(dtOut);
- dgCommunications.DataSource = dtOut;
- }
- catch (InvalidOperationException)
- {
- }
- catch (SqlException)
- {
- }
- }
- }
- SqlDataAdapter adp = new SqlDataAdapter();
- using (SqlConnection connection = new SqlConnection(conString))
- {
- try
- {
- switch (cmbCategory.Text)
- {
- case "All":
- break;
- }
- }
- catch (InvalidOperationException)
- {
- }
- catch (SqlException)
- {
- }
- }
- class DAL
- {
- private readonly string _connectionString;
- public DAL(string connectionString)
- {
- _connectionString = connectionString;
- }
- public DataTable SearchAll(string searchText)
- {
- var sql = "SELECT * FROM tblCommunication "+
- "WHERE LetterType = @searchText "+
- "OR LetterNumber = @searchText "+
- "OR LetterAmount = @searchText "+
- "OR LetterFrom = @searchText "+
- "OR LetterTo = @searchText "+
- "OR ReceivedBy = @searchText "+
- "OR Requisition = @searchText "+
- "OR LetterSubject = @searchText "+
- "OR LetterContent = @searchText "+
- "OR LetterRemarks = @searchText";
- var parameter = new SqlParameter("@searchText", SqlDbType.VarChar);
- parameter.Value = searchText;
- return GetDataTable(sql, CommandType.Text, parameter);
- }
- public DataTable SearchIncomingCommunications(string caterogy, string searchText)
- {
- // implementation same as the example above
- throw new NotImplementedException();
- }
- public DataTable SearchInsideCommunications(string caterogy, string searchText)
- {
- // implementation same as the example above
- throw new NotImplementedException();
- }
- // add other methods as well
- private DataTable GetDataTable(string sql, CommandType commandType, params SqlParameter[] parameters)
- {
- var dt = new DataTable();
- using (var con = new SqlConnection(_connectionString))
- {
- using (var cmd = new SqlCommand(sql, con))
- {
- cmd.CommandType = commandType;
- foreach (var parameter in parameters)
- {
- cmd.Parameters.Add(parameter);
- }
- using (var da = new SqlDataAdapter(cmd))
- {
- da.Fill(dt);
- }
- }
- }
- return dt;
- }
- }
Add Comment
Please, Sign In to add comment