Click here to Skip to main content
15,907,236 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,
I have developed one grid view. when was clicked Edit it showing the error "The connection was not closed. The connection's current state is open.".



My code is below:
C#
protected void gvreservation_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
        string name = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerName")).Text;
        string age = ((TextBox)gvreservation.FooterRow.FindControl("txtFAge")).Text;
        string location = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFLocation")).Text;
        string class1 = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFClass")).Text;
        string sqlstatement = "update reservation set CustomerName=@CustomerName,Age=@Age,Location=@Location,Class=@Class where CustomerID=@CustomerID";
        ocon.Open();
        SqlCommand cmd = new SqlCommand(sqlstatement,ocon);
        cmd.Parameters.Add("@CustomerID",id);
        cmd.Parameters.Add("@CustomerName",name);
        cmd.Parameters.Add("@Age",age);
        cmd.Parameters.Add("@Location",location);
        cmd.Parameters.Add("@Class",class1);
        cmd.ExecuteNonQuery();
        ocon.Close();

    }
Posted

Remove the ocon.Open(); line in your source code. You only need it when your connection is closed.
 
Share this answer
 
When you open the connection (ocon.Open), check the ocon.ConnectionState and open it only if the connection state is closed.
 
Share this answer
 
v3
Generally this error occurs when connection is already opened and you are trying to open it again
so...
Place all your code in try block.
In Finally block write
ocon.close()

so that when exception occurs your connection will be close.
 
Share this answer
 
v2
Comments
KiranBabu M 26-Sep-11 5:49am    
elegant solution...!
if the connection is already opened then such error will come.
do one thing to rectify that...
in 2 ways you can implement or rectify it

first way
if (ocon.State == ConnectionState.Open)
{
ocon.Close();
}
ocon.Open();

second way
if (ocon.State == ConnectionState.Close)
{
ocon.Open();
}
 
Share this answer
 
v2
Dear sir,
when i execute the program there is one more record added in grid but not updated when i was clicking edit link button.
code:


private void Binddata()
{
string selectSQL = "SELECT * FROM reservation";
ocon.Open();
SqlCommand cmd = new SqlCommand(selectSQL, ocon);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

adapter.Fill(ds, "reservation");
ocon.Close();
gvreservation.DataSource = ds;
gvreservation.DataBind();
}

protected void gvreservation_RowCommand(object sender, GridViewCommandEventArgs e)
{
string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
string name = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerName")).Text;
string age = ((TextBox)gvreservation.FooterRow.FindControl("txtFAge")).Text;
string location = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFLocation")).Text;
string class1 = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFClass")).Text;
ocon.Open();
string sqlstatement ="Insert into reservation(CustomerID,CustomerName,Age,Location,Class)values(@CustomerID,@CustomerName,@Age,@Location,@Class)";
SqlCommand cmd = new SqlCommand(sqlstatement,ocon);
cmd.Parameters.Add("@CustomerID",id);
cmd.Parameters.Add("@CustomerName",name);
cmd.Parameters.Add("@Age",age);
cmd.Parameters.Add("@Location",location);
cmd.Parameters.Add("@Class",class1);
cmd.ExecuteNonQuery();
ocon.Close();
Binddata();
}
protected void gvreservation_RowEditing(object sender, GridViewEditEventArgs e)
{
string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
string name = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerName")).Text;
string age = ((TextBox)gvreservation.FooterRow.FindControl("txtFAge")).Text;
string location = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFLocation")).Text;
string class1 = ((DropDownList)gvreservation.FooterRow.FindControl("ddlFClass")).Text;
string sqlstatements = "update reservation set CustomerName=@CustomerName,Age=@Age,Location=@Location,Class=@Class where CustomerID=@CustomerID";
ocon.Open();
SqlCommand cmd = new SqlCommand(sqlstatements,ocon);
cmd.Parameters.Add("@CustomerID",id);
cmd.Parameters.Add("@CustomerName",name);
cmd.Parameters.Add("@Age",age);
cmd.Parameters.Add("@Location",location);
cmd.Parameters.Add("@Class",class1);
cmd.ExecuteNonQuery();
ocon.Close();

}
protected void gvreservation_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = ((TextBox)gvreservation.FooterRow.FindControl("txtFCustomerID")).Text;
string sqlstatement = "Delete from reservation where CustomerID=@CustomerID";
ocon.Open();
SqlCommand cmd = new SqlCommand(sqlstatement,ocon);
cmd.Parameters.Add("@CustomerID",id);
cmd.ExecuteNonQuery();
ocon.Close();
Binddata();
}
}
 
Share this answer
 

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