Click here to Skip to main content
14,270,361 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello guys , i have this database https://ibin.co/2phSKY8PfVSs.png[^]
i want to search for babies with last name "Smith" and their gender is "Male"
i have a textbox for entering lastname and a combobox to choose gender and a button to get the result, when i press the button it only gets the name and neglect the gender condition.
here is my code :

private DataTable tb1 = new DataTable();


private DataTable GetTbb()
        {
            //  DataTable tb1 = new DataTable();
            string conectingstrng = ConfigurationManager.ConnectionStrings["hsmcntr.Properties.Settings.tbbConnectionString"].ConnectionString;
            using (OleDbConnection con = new OleDbConnection(conectingstrng))
            {
                using (OleDbCommand cmd = new OleDbCommand("Select *from Table1", con))
                {
                    con.Open();
                    OleDbDataReader reader = cmd.ExecuteReader();
                    tb1.Load(reader);

                }
            }
            return tb1;

        }

private void GETRSLT(string Nme, string Gndr, ComboBox nme, ComboBox gndr)
       {
           DataView dvtble = tb1.DefaultView;
           dvtble.RowFilter = Nme + "Like '%" + nme.SelectedItem + "%'" + Gndr + "Like '%"  + gndr.SelectedItem + "%'";
       }


private void button1_Click(object sender, EventArgs e)
        {   
GETRSLT("Nme","Gndr",textbox1,combobox1)}


What I have tried:

Tried everthing in my mind but to no avail.
searched YouTube and google and didn't find anything related to my question.
Posted
Updated 28-Jul-16 21:27pm
Comments
0x01AA 29-Jul-16 3:15am
   
Try
dvtble.RowFilter = Nme + "Like '%" + nme.SelectedItem + "%'" + " AND " + Gndr + "Like '%" + gndr.SelectedItem + "%'";

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You Need to concatenate the two boolean Expression (Nme like %x%, Gndr like %y%) by a Logical Operator, in this case it is "AND".
dvtble.RowFilter = Nme + "Like '%" + nme.SelectedItem + "%'" + " AND " + Gndr + "Like '%"  + gndr.SelectedItem + "%'";

Here you will find a nice summary about filter Syntax:
DataView RowFilter Syntax [C#][^]
I hope it helps.

[Edit] There are also missing spacesbefore "Like" operator
dvtble.RowFilter = Nme + " Like '%" + nme.SelectedItem + "%'" + " AND " + Gndr + " Like '%"  + gndr.SelectedItem + "%'";
   
v3
Comments
Integra Belm 29-Jul-16 3:57am
   
thanx for the link it really useful for increasing my knowledge , as for the code now it gives me An unhandled exception of type 'System.Data.SyntaxErrorException' occurred in System.Data.dll

Additional information: Syntax error: Missing operand after ''%Smith%'' operator.
0x01AA 29-Jul-16 4:00am
   
Can you post here the actual filter Expression for which the exception occurs?
Integra Belm 29-Jul-16 4:11am
   
private void GETRSLT(string Nme, string Gndr, TextBox nme, ComboBox gndr)
{
DataView dvtble = tb1.DefaultView;
dvtble.RowFilter = Nme + "Like '%" + nme.Text + "%'" + " AND " + Gndr + "Like '%" + gndr.SelectedItem + "%'";
}

private void button1_Click(object sender, EventArgs e)
{

GETRSLT("Last_Name", "Gender", textBox1, comboBox1);
}
0x01AA 29-Jul-16 4:14am
   
I mean posting really the final string you assign to dvtble.RowFilter.
Integra Belm 29-Jul-16 4:18am
   
I enter the word "Smith" in the textbox and i select "Female" from the comboBox.
0x01AA 29-Jul-16 4:23am
   
I think some space are missing, I mark it with "*" below:
dvtble.RowFilter = Nme + "*Like '%" + nme.Text + "%'" + " AND " + Gndr + "*Like '%" + gndr.SelectedItem + "%'";
}
Integra Belm 29-Jul-16 4:47am
   
ok i'm going to try that , thanx in advance :D.
Integra Belm 29-Jul-16 5:05am
   
ok now it get the required results when the comboBox is set to "Female" but when it set to "Male" it get both female and male results.
0x01AA 29-Jul-16 5:14am
   
Great, happy coding :-)
[Edit]
Sorry I thought it is solved.... :-)
Now why it Shows also felmale? Think about "female" in the data filtered by "%male%", it matches perfect ;)

"Gndr = '" + gndr.SelectedItem + "'" will solve that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100