Searching with multiple textboxes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • M1kkelZU
    New Member
    • Feb 2013
    • 80

    Searching with multiple textboxes.

    So I've got a button that has to use 2 textboxes to search for 2 columns in my database.

    Code:
     private void btnFilter_Click(object sender, EventArgs e)
            {
                SQLiteDataAdapter adap = new SQLiteDataAdapter("select * from Test", GetConnection());
    
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dgvTable.DataSource = ds.Tables[0];
    
                DataTable dt = (DataTable)dgvTable.DataSource;
                if (txtFilterChannel.Text != null && txtFilterLog.Text != null)
                {
                    dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
                    dt.DefaultView.RowFilter = "LogName like '%" + txtFilterLog.Text + "%'";
                }
                else if (txtFilterChannel.Text != null && txtFilterLog.Text == null)
                {
                    dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
                }
                else if (txtFilterChannel.Text == null && txtFilterLog.Text != null)
                {
                    dt.DefaultView.RowFilter = "Logname like '%" + txtFilterLog.Text +"%'";
                }
    
                CloseConnection();
            }
    My Current code for it.

    The GetConnection() ; and the CloseConnection (); Methods are just making a new connection and closing that connection, if needed I can post that code too later.

    But basically, It only "filters" 1 textbox but if I say I want to search the Channel and the Log in which it is in, it doesn't search anything and returns all data in the database.

    Any ideas? Or am I using the wrong way to show this?
  • jin ariel
    New Member
    • Jun 2013
    • 1

    #2
    Code:
    rivate void btnFilter_Click(object sender, EventArgs e)
            {
                SQLiteDataAdapter adap = new SQLiteDataAdapter("select * from Test", GetConnection());
     
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dgvTable.DataSource = ds.Tables[0];
     
                DataTable dt = (DataTable)dgvTable.DataSource;
                if (txtFilterChannel.Text != null && txtFilterLog.Text != null)
                {
                    dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
                    dt.DefaultView.RowFilter = "LogName like '%" + txtFilterLog.Text + "%'";
                }
                else if (txtFilterChannel.Text != null && txtFilterLog.Text == null)
                {
                    dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
                }
                else if (txtFilterChannel.Text == null && txtFilterLog.Text != null)
                {
                    dt.DefaultView.RowFilter = "Logname like '%" + txtFilterLog.Text +"%'";
                }
     
                CloseConnection();
    Last edited by Rabbit; Jun 4 '13, 03:14 PM. Reason: Please use code tags when posting code.

    Comment

    • M1kkelZU
      New Member
      • Feb 2013
      • 80

      #3
      bump lol. Jin just reposted my exact code xD

      Comment

      Working...