I keep getting this error randomly:
System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: The connection was not closed. The connection's current state is connecting.
The code it's complaning about is below:
DataSet ds = new DataSet();
cn = new SqlConnection(GetDBConnectionString());
using (cn)
{
try
{
SqlCommand cmd = new SqlCommand("uspGetNavigationItems", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds, "NavItems");
}
catch (Exception ex)
{
ds = null;
throw ex;
}
finally
{
if (cn.State != ConnectionState.Closed)
{
cn.Close();
}
}
}
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
else
{
return null;
}
I don't understand where the problem is, why it's saying the connection is connecting, when I have a finally to clean it up. Is it because i'm using Finally to close and the using statement, which is supposed to close it as well? Again this happens randomly not always, that's why i'm not sure what's going on.
Thank you.