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();
Related videos on Youtube
Author by
Binier
Updated on June 08, 2022Comments
-
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?
-
GarethDHave 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 over 10 yearsgridview1.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 over 10 yearsI removed that already, check my updated answer. you will get the exception in catch, put a break point in catch block.
-
Adil over 10 yearsYou probably need to put the try catch in side using block, I have updated the answer.
-
Binier over 10 yearsI tried to use try catch inside using block, but it doesn't work. Gridview doesn't show any data.
-
Adil over 10 yearsCan you see data in datatable after fill method?
-
Binier over 10 yearsIt 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 over 10 yearsDid 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 over 10 yearsMake some change in code to check if you get that change in the rendered page, or you would need to publish it again