I am working in visual studio 2010 creating a ASP web application. I am trying to implement a search function with a grid view, where I will be passing in an ADO select command into a dataset and filling up the datagrid with my desired SQL. I know this is possible for winforms and datagridviews but for some reason, it is acting strangely on my ASP application. Here is some code...
protected void btn_search_Click(object sender, EventArgs e)
{
SqlConnection cs = new SqlConnection("Data Source=WILSON-PC; Initial Catalog=KamManOnline; Integrated Security=TRUE");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = new SqlCommand("SELECT * FROM vw_orderView WHERE Supplier = @Supplier", cs);
da.SelectCommand.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = ddl_SupplierView.Text.ToString();
ds.Clear();
da.Fill(ds);
gv_search.DataSource = ds.Tables[0];
gv_search.DataBind();
}
What happens is it picks out the right amount of data but does not show it on the grid view. For example, say if I had 3 fields as Wilson for the supplier. When I click my search button, it would bring out 3 rows but they are empty. Any ideas or another way to walk around this? Thanks!
P.S. I tried using the databind wizard but eventrually I want more flexability in 开发者_Python百科my search, for example
SELECT * FROM vw_orderView WHERE (Supplier = @Supplier OR @Supplier IS NULL).
I tried writing this in the query builder but I need to add some if condition to say whether supplier will have a value or be null (check box). If this is the right approach, then please say so. Thanks
I would do the following to troubleshoot.
- Run the query directly from SSMS to verify that you get back the results you expect.
- Make sure the GridView has AutoGenerateColumns=true if you are not manually defining Columns.
- Run in debug mode with a breakpoint set on
ds.Clear()
and inspect the freshly added Parameter to see that it contains the value you expect.
精彩评论