private void button4_Click(object sender, EventArgs e)
        {
            {
                SqlConnection conn = new SqlConnection("Data Source=user-PC;Initial Catalog=URBAN-AUTO-CLINIC;Integrated Security=True;User Instance=False");
                SqlDataAdapter adaptor = new SqlDataAdapter();
                DataTable table = new DataTable();
                DataSet ds = new DataSet();
                adaptor.SelectCommand = new SqlCommand("SELECT FirstName,LastName FROM ClientNCar WHERE FirstName ='" +txtFName.Text +"'AND LastName='"+txtLName.Text+"'",conn);
                adaptor.Fill(ds);
                ClientSearchDisplay.DataSource = ds.Tables[0];


            }
        }

`Hie all My name is Eric.

with the above code I am trying to searc for a record usinf textboxes but all I get is empty columns after the button is event is performed

Recommended Answers

All 5 Replies

Have you tried simpler test cases, such as SELECT FirstName, LastName FROM ClientNCar to ensure the connection and column names are all correct? Also, is your database case-sensitive, and if so, are you accouting for that?

Another thing, be careful with that code, I don't think SqlCommand makes any effort to prevent SQL injections in your database. I'm pretty sure SqlCommandBuilder does, so you may want to look into it down the road.

this. ClientSearchDisplay.DataSource = ds.Tables[0];

i hope ClientSearchDisplay is a data grid view.
(1)Try usingthis. ClientSearchDisplay.DataSource = ds.Tables[0];

(2)check the query result first by only first name. May be there are no records that satisfy the AND condition.
Kindly let me know if it helped

Hie Mailet thank you for taking your time and effort in helping with this problem I have tried several queries and the worked well on the database that I am querying.the code above returns me the columns I queried, but with no values matching those I entered from the two textboxes.thats what gives a headache

Try this

private void button4_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=user-PC;Initial Catalog=URBAN-AUTO-CLINIC;Integrated Security=True;User Instance=False");
    DataSet ds = new DataSet();
    SqlCommand adaptor = new SqlCommand("SELECT FirstName,LastName FROM ClientNCar WHERE [FirstName] ='" +txtFName.Text.Trim() +"' AND [LastName]='"+txtLName.Text.Trim()+"'",conn);
    SqlDataAdapter adp=new SqlDataAdapter(cm);
    adaptor.Fill(ds);
    ClientSearchDisplay.DataSource = ds.Tables[0];
}

Couple questions: are you using MS SQL Server, or something else? And is the data type variable length (varchar or nvarchar) or fixed length (char or nchar)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.