I’m trying to search for records in my VSC database through the SQL connection string and display records based on the user’s input. I seem to be hitting a wall with the tutorials I’ve found on how to get things running. At the moment this code is throwing an exception with sda.Fill(dt);
. Could anyone point out where I’m going wrong?
protected void GoButton_Click(object sender, EventArgs e)
{
SqlCommand command;
SqlConnection conn;
String selectTable;
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
selectTable = "SELECT * from Activity where ActivityName LIKE '%'+@ActivityName+'%'";
command = new SqlCommand(selectTable, conn);
command.Parameters.AddWithValue("ActivityName", SearchBox);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(command);
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
command.Dispose();
conn.Close();
2
Answers
You missed .Text on SearchBox that’s why it won’t come and i revamped your code use code always like this
Ok, the approach to adding filter (1 or many) can be attacked like this:
markup:
Now code to load grid is this:
And now we have this:
Now, you might just have ONE filter – one text box. But here is the code and approach for several filters like I have in above.
So, follow this template and design pattern:
So, we can have with above 1, 2 or 9 filter options.
And our clear filter button code
So, I can for example type in K to find all hotels that start with K.
and we get this: