3
Reply

Asp.net Web Application

Murugavel S

Murugavel S

Jul 17 2015 1:38 AM
296
Hi Friendss
 
         i created one web form contains textboxes ,one gridview and button..
         when click the button the values in the textboxes should add into the db and display in gridview.. for that i created two functions..
         one is to insert values in db and other is to display db values into gridview.. 
My Question is to how display the db values into gridview by button click event.. err- invalid  attempt to call Read when reader is closed.
============= 
Below function is to add values to DB:
   
private void loadTable()
{
   SqlConnection con = null;
   try
   {
      SqlConnectionStringBuilder connectionString = getConString();
      con = new SqlConnection();
      con = new SqlConnection(connectionString.ToString());
      con.Open();
      string sqlQry = "insert Query";
       SqlCommand cmd = new SqlCommand(sqlQry, con);
      SqlDataReader reader;
      reader = cmd.ExecuteReader();
      gridview.DataSource = reader;
      gridview.DataBind();
   }
   catch (Exception ex)
   {
   }
   finally
   {
      con.Close();
   }
}
 ======================
 Below Function is to display the db Values into Gridview:
private void loadGrid()
{
   SqlConnection con = null;
   try
   {
      SqlConnectionStringBuilder connectionString = getConString();
      con = new SqlConnection();
      con = new SqlConnection(connectionString.ToString());
      con.Open();
      string sqlQry = "select * from table";
      SqlCommand cmd = new SqlCommand(sqlQry, con);
      cmd.CommandType = CommandType.Text;
      SqlDataAdapter sda = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      sda.Fill(ds);
      gridview.DataSource = ds;
      gridview.DataBind();
   }
   catch (Exception ex)  here throwing error "invalid attempt to call read when reader is closed"
   {
   }
   finally
   {
      con.Close();
   }
}
 
button click event
   {    
      loadTable();      
      loadGrid();
   }     
 
the error in loadgrid functions in only in button click event, the same loadgrid function is also in page load form.. 
Thanks in advance 
 

Answers (3)