Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :

C#
private DataTable tb1 = new DataTable();


C#
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;

        }

C#
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 + "%'";
       }


C#
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
[no name] 29-Jul-16 3:15am    
Try
dvtble.RowFilter = Nme + "Like '%" + nme.SelectedItem + "%'" + " AND " + Gndr + "Like '%" + gndr.SelectedItem + "%'";

1 solution

You Need to concatenate the two boolean Expression (Nme like %x%, Gndr like %y%) by a Logical Operator, in this case it is "AND".
C#
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
C#
dvtble.RowFilter = Nme + " Like '%" + nme.SelectedItem + "%'" + " AND " + Gndr + " Like '%"  + gndr.SelectedItem + "%'";
 
Share this answer
 
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.
[no name] 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);
}
[no name] 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900