Why GridView not showing record in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

hello all ,

i have a botton , and the code behind it as below :

 

protected void btnView_Click(object sender, EventArgs e)
{
    SqlDataAdapter sda = new SqlDataAdapter("Select * from tblMain", conn);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    gv2.DataSource = dt;
    conn.Close();
}

 

when i run the web page and click the btnview .. the page is still empty ?!!

why is that 

 

the other question is :

if i want to refresh the data of the gv2, automatically after ading data to it how to do it ?

 

regards

 

Posted one year ago Modified on one year ago
siamandm says:
protected void btnView_Click(object sender, EventArgs e)
       {
 
           
 
 
 
           SqlDataAdapter sda = new SqlDataAdapter("Select * from tblMain", conn);
           DataTable dt = new DataTable();
           sda.Fill(dt);
           gv2.DataSource = dt;
           conn.Close();
 
 
       }

Change the above with the below code.

protected void btnView_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(constr))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("Select * from tblMain"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                gv2.DataSource = dt;
                gv2.DataBind();
            }
        }
        conn.Close();
    }
}

You are missing this line gv2.DataBind();

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html