Romio Durak Romio Durak - 7 months ago 8
SQL Question

How to get data from server into Datagridview C#

I want to get some Data displayed at a DataGridview.

Here is what I tryed so far :

cn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * FROM Arbeiter WHERE (Name Like '%" + tbSuche.Text + "%'", cn);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;


I get the error :


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


it works without the WHERE part ... so I think the error should be there .

Thanks for your help

Answer

You did not close your parenthesis in the sql query. Should be:

SqlDataAdapter sda = new SqlDataAdapter("select * FROM Arbeiter WHERE (Name Like '%" + tbSuche.Text + "%')", cn);

I always like to put mine on a new line like:

string sql = "select * FROM Arbeiter WHERE (Name Like '%" + tbSuche.Text + "%'";
SqlDataAdapter sda = new SqlDataAdapter(sql, cn);

That way I can always put in a breakpoint before execution and copy the SQL statement over to SQL Management Studio to run it there to check the results.

Comments