I would like to show data in a table in SQL server database to Gridview in ASP.NET project, but the gridview shows nothing. Could you please help me to find where is the problem, and how to fix it.
Thanks
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string cs = ConfigurationManager.ConnectionStrings["SampleDBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from newexample", con);
DataTable dt = new DataTable();
dt.Columns.Add("col0");
dt.Columns.Add("col1");
dt.Columns.Add("col2");
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DataRow dr = dt.NewRow();
dr["col0"] = rdr["col0"];
dr["col1"] = rdr["col1"];
dr["col2"] = rdr["col2"];
dt.Rows.Add(dr);
}
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}