Consider the following code:
SqlConnection conn = new SqlConnection(@"connection string");
SqlCommand ourCommand = new SqlCommand(String.Format(
@"SELECT Field1,Field2,Field3 FROM Tbl WHERE Field1 LIKE '@FL1'"), conn);
ourCommand.CommandTimeout = 6000;
ourCommand.Parameters.AddWithValue("@FL1", TextBox1.Text);
SqlDataAdapter adapter = new SqlDataAdapter(ourCommand);
DataTable dt = new DataTable();
conn.Open();
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
The problem is that datatable is empty – which means the command either was not executed or incorrect query was generated. What am I missing? Connection and query are valid. The command without parameter also works. Database engine is SQL Server 2008 R2
You’ve put the parameter name in quotes, so it’s being treated as a value, not as a parameter. Try this instead:
(I’d actually expect your existing code to throw an exception given that you’re supplying more parameters than are in the SQL…)
As noted in comments, you don’t need the
string.Formateither.