0

Possible Duplicate:
how to make search of a string in a data base in c#

I want to search for a name in details .. thats the code for search button

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = Tyres.Properties.Settings.Default.Database1ConnectionString;
    SqlConnection conn = new SqlConnection(connectionString);
    DataTable dt = new DataTable();
    SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Details like" + textBox1.Text, conn);
    SDA.Fill(dt);
    dataGridView1.DataSource = dt;
}

and this example of my application Click here to see the image

I am getting the error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: An expression of non-boolean type specified in a context where a condition is expected, near 'likeelie'.

I have tried this code to search for a integer(like Quantity) it works well for me :

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = Tyres.Properties.Settings.Default.Database1ConnectionString;
    SqlConnection conn = new SqlConnection(connectionString);
    DataTable dt = new DataTable();
    SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Quantity like" + int.parse(textBox1.Text), conn);
    SDA.Fill(dt);
    dataGridView1.DataSource = dt;
}

so I need a help with the first code

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Elie M
  • 263
  • 1
  • 3
  • 15
  • You've asked basically the same question twice in a 3 hour span. http://stackoverflow.com/questions/14695282/how-to-make-search-of-a-string-in-a-data-base-in-c-sharp. Don't do that – Wim Ombelets Feb 04 '13 at 20:51

1 Answers1

5

Looks like you need to add a space in your SQL statement, you are just appending the value of textBox1.Text without a space, so the value of textBox.Text is becoming one word with like.

SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Details like " + textBox1.Text, conn);
JG in SD
  • 5,427
  • 3
  • 34
  • 46
  • I would also recommend using SqlParameters [HERE](http://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.100).aspx) – user959729 Feb 04 '13 at 19:24
  • now i got another probleme : An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Invalid column name 'Audi'. – Elie M Feb 04 '13 at 19:42
  • 1
    @user1951045 you should post another question to get that. Not sure how you'd get an invalid column name from what you've posted. It is possible that whatever you are providing through `textBox1` is the source of the issue. Dynamically built SQL can be tricky, and if the input isn't sanitized they allow SQL Injection Attacks, so store procedures are preferred. – JG in SD Feb 04 '13 at 19:55
  • He gets that error because he puts a plain unquoted string in his SQL. So his input it treated like a column name. – ThiefMaster Feb 14 '13 at 13:21