Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 string Sno = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtSno")).Text;
        string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtName")).Text;
        string Age = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtage")).Text;
        string Address = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress")).Text;
        SqlConnection con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Details set Sno=@Sno, Name=@Name," +
     "Age=@Age where Sno=@Sno;" +
     "select Sno,Name,Age,Address from Details";
        cmd.Parameters.Add("@Sno", SqlDbType.Int).Value = Sno;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
        cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = Age;
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        SqlCommand cmd1 = new SqlCommand("Select * from Details", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd1);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
}

The Error occurs as follows; The connection was not closed. The connection's current state is open.

please help me. from my above code what is the problem.
Posted
Updated 30-Nov-12 20:27pm
v2

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 string Sno = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtSno")).Text;
        string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtName")).Text;
        string Age = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtage")).Text;
        string Address = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress")).Text;
        SqlConnection con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Details set Sno=@Sno, Name=@Name," +
     "Age=@Age where Sno=@Sno;" +
     "select Sno,Name,Age,Address from Details";
        cmd.Parameters.Add("@Sno", SqlDbType.Int).Value = Sno;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
        cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = Age;
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        SqlCommand cmd1 = new SqlCommand("Select * from Details", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd1);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        
        GridView1.DataSource = ds;
        GridView1.DataBind();
con.Close(); // it comes here
}
 
Share this answer
 
use this code
C#
if(con.State = ConnectionState.Closed)
con.open

instead of your code
C#
con.Open






Actually you are calling con.open() method twice. So delete con.open() next to the declaration of "con".
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900