Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The connection was not closed. The connection's current state is open.


Where i am wrong?
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    conn.Open();

    var ddl = (DropDownList)e.Row.FindControl("ddlCompany1");

    SqlCommand cmd = new SqlCommand("SELECT CompanyName FROM  Company", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    conn.Close();

    ddl.DataSource = ds;
    ddl.DataTextField = "CompanyName";
    ddl.DataValueField = "ID";
    ddl.DataBind();
    ddl.Items.Insert(0, new ListItem("Select", "0"));
  }
}
Posted
Updated 13-Dec-14 23:41pm
v2
Comments
Maciej Los 14-Dec-14 8:03am    
Please, carefully read error message. ;)

The error message says that connection is still opened.
C#
//below line causes error message:
conn.Open();

You need to check connection state[^].
C#
if (conn.State==ConnectionState.Closed) {conn.Open();}
 
Share this answer
 
Comments
DamithSL 14-Dec-14 8:27am    
Maciej, what if the connection status is one of this "Broken, Connnecting, Executing, Fetching" :)
Maciej Los 14-Dec-14 9:20am    
In this case - nothing ;)
DamithSL 14-Dec-14 10:58am    
5wd
Maciej Los 14-Dec-14 11:01am    
Thank you, Damith (i hope it's your name) ;)
DamithSL 14-Dec-14 11:02am    
Yes :)
I would do as below,

C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
      var ddl = (DropDownList)e.Row.FindControl("ddlCompany1");
      if(ddl !=null)
      {
          DataSet ds = GetData("SELECT CompanyName, ID FROM  Company");
          ddl.DataSource = ds.Tables[0];
          ddl.DataTextField = "CompanyName";
          ddl.DataValueField = "ID";
          ddl.DataBind();
          ddl.Items.Insert(0, new ListItem("Select", "0"));
      }
   }
}

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    using (SqlCommand cmd = new SqlCommand(query,con))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        {
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
    }
}


Note that,
1. try to separate your UI code and the data access codes
2. Reusable methods
3. you haven't select ID column in your select statement but you set it as value field!
4. Connections handling is not easy when you have class level or global shared connection object, try to use "using blocks" and create the connection when you need and it will close the connection automatically.
 
Share this answer
 
v4
Comments
Maciej Los 14-Dec-14 11:04am    
Good advice, +5!
Please, add S to complete SELECT statement:
DataSet ds = GetData("SELECT CompanyName, ID FROM Company");
DamithSL 14-Dec-14 11:06am    
Oh.. thanks, Maciej

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