5
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?

8
  • Have you tried debugging and checking that there is actually data in the table when it is being data bound? Commented Dec 31, 2013 at 9:32
  • Yes I have tried but in windows forms. Commented Dec 31, 2013 at 9:35
  • Is it correct id GridView1 Commented Dec 31, 2013 at 9:41
  • 1
    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? Commented Dec 31, 2013 at 9:44
  • See my answer. i hopes it may helps you stackoverflow.com/a/20855275/2218635 Commented Dec 31, 2013 at 9:48

6 Answers 6

1

You could be getting exception in DataBind() - use a try-catch block. This could happen due to a missing column in the DataTable that is being used in a bound field.

 protected void Button1_Click1(object sender, EventArgs e)
 {

   SqlConnectionStringBuilder connb = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["SCS"].ConnectionString);
   using (SqlConnection conn = new SqlConnection(connb.ConnectionString))
   {
    try
    {
      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();
      GridView1.Rows[0].Cells[0].Text. = "a";
    }
    catch(Exception ex)
    {
         Response.Write(ex.Message);
    }     
   }           
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
I removed that already, check my updated answer. you will get the exception in catch, put a break point in catch block.
You probably need to put the try catch in side using block, I have updated the answer.
I tried to use try catch inside using block, but it doesn't work. Gridview doesn't show any data.
Can you see data in datatable after fill method?
|
1
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Comments

1

Seems like your query never gets executed

cmd.ExecuteNonQuery () 

This should have happened before this line

SqlDataAdapter da = new SqlDataAdapter(cmd); 

Comments

0
  • You should set AutoGenerateColumns="false", if you want to bind specify column.Please check it

  • And also need to check have you set visible="false" or style="display:none" in your gridview

  • And check DataField must matched the table column name

Comments

0

I had a problem where I was getting data in the datasource and the GridView wasn't showing up at all. I had AutoGenerateColumns = "true" in the Gridview definition on the page. For some reason, when I set up a breakpoint and looked at the properties of the Gridview in the code behind, it was still set to false. I set it to true in the code behind before setting the Gridview equal to the datasource and doing the databind. That worked.

Comments

-1
       SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
       SqlDataAdapter da = new SqlDataAdapter(cmd);      
       ds = new DataSet();
       da.Fill(ds, "usersdata");
       GridView1.DataSource = ds.Tables["usersdata"];
       GridView1.DataBind();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.