How to Bind a GridView from database

前端 未结 6 2056
梦谈多话
梦谈多话 2020-12-11 19:24

How to bind a GridView?

I want to display my table data in a gridview.

I have createed SQL table EmpDetail with columns ID, Name, Salary D

6条回答
  •  北海茫月
    2020-12-11 20:27

    try this....

     protected void Page_Load(object sender, EventArgs e)
    {
        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("select * from Table1", conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            conn.Close();
        }
    
    }
    
    

提交回复
热议问题