简体   繁体   中英

ASP.net weird grid view behavior

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!

PS I tried using the databind wizard but eventrually I want more flexability in 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.

  1. Run the query directly from SSMS to verify that you get back the results you expect.
  2. Make sure the GridView has AutoGenerateColumns=true if you are not manually defining Columns.
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM