asp.net gridview doesn't display data

13,896

Solution 1

Seems like your query never gets executed

cmd.ExecuteNonQuery () 

This should have happened before this line

SqlDataAdapter da = new SqlDataAdapter(cmd); 

Solution 2

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Share:
13,896

Related videos on Youtube

Binier
Author by

Binier

Updated on June 08, 2022

Comments

  • Binier
    Binier almost 2 years
    protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnectionStringBuilder connb = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["SCS"].ConnectionString);      
        using (SqlConnection conn = new SqlConnection(connb.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
            DataTable tb = new DataTable();
    
            SqlDataAdapter da = new SqlDataAdapter(cmd);
    
            da.Fill(tb);
            tb.AcceptChanges();
            GridView1.DataSource = tb;
            GridView1.DataBind();
         }
    }
    

    This is my code in C# asp.net application. I want to display SQL table in gridview.

    <Columns>
      <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="name" />
      <asp:BoundField ItemStyle-Width="150px" DataField="lastname" HeaderText="lastname" />
      <asp:BoundField ItemStyle-Width="150px" DataField="ID" HeaderText="ID" />
    </Columns>
    

    IT shows empty gridview(When I press button1). It doesn't shows any error message. Connection string works, SQL command affects rows, But it still doesn't show any data on gridview!!!

    Can anyone help me?

    • GarethD
      GarethD
      Have you tried debugging your actual web page to ensure that button1 is correctly hooked up to the method, and to ensure that there are rows in tb?
  • Binier
    Binier over 10 years
    gridview1.rows[0].cells[0].text _ it was extra(I just tried. I wanted to see what would happen). I deleted this but here isn't problem. problem is that datagridview doesn't show any data which is in my sql server.
  • Adil
    Adil over 10 years
    I removed that already, check my updated answer. you will get the exception in catch, put a break point in catch block.
  • Adil
    Adil over 10 years
    You probably need to put the try catch in side using block, I have updated the answer.
  • Binier
    Binier over 10 years
    I tried to use try catch inside using block, but it doesn't work. Gridview doesn't show any data.
  • Adil
    Adil over 10 years
    Can you see data in datatable after fill method?
  • Binier
    Binier over 10 years
    It is already in my site and I am just changing the code from visual studio(File-> open -> web site). Can it be a problem?
  • Adil
    Adil over 10 years
    Did you publish the site make a library? if not then save the page. You can ensure but putting some syntax error on page but be careful if it is live site.
  • Adil
    Adil over 10 years
    Make some change in code to check if you get that change in the rendered page, or you would need to publish it again