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?
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Seems like your query never gets executed
cmd.ExecuteNonQuery ()
This should have happened before this line
SqlDataAdapter da = new SqlDataAdapter(cmd);
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);
}
}
}
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
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.
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();
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.