Tuesday, March 20, 2012

Connections Closed but still getting errors on page

I have a page that I have 3 connections. I've made sure that each of these are closed when they are not being used and opened just right before being used. I keep getting the error "There is already an open DataReader associated with this Command which must be closed first." This error might show up as being produced by a dataadapter or sqldatareader...I have many. I've even tried to make separate connections as some have mentioned for each...leaving me with 15+ connections. I have added "MultipleActiveResultSets=True" to the connection strings as some have mentioned. I just don't know where to go from here...

Is it possible that the problem lies in multiple instances of this page being opened? Also, the data refreshes every 15 seconds. I really need this to work, but I have no clue on how to fix this problem. The error is easy to reproduce by opening up multiple instances, but some of the times is doesn't give an error at all?!

could you provide some example code how you execute your process when your get error?

|||

This section has showed up as producing an error...

 SqlCommand SqlCommand_ACK =new SqlCommand("SQL_STATEMENT", Connection_dis01_8);if (Connection_dis01_8.State == ConnectionState.Closed) Connection_dis01_8.Open(); SqlCommand_ACK.ExecuteNonQuery(); //<<<<<<<<ERROR HERE<<<<<<<<if (Connection_dis01_8.State == ConnectionState.Open) Connection_dis01_8.Close();

This section has also showed up...

 SqlDataReader SqlReader_OOME; SqlCommand SqlCommand_OOME =new SqlCommand("SQL_STATEMENT", Connection_dis01_6);if (Connection_dis01_6.State == ConnectionState.Closed) Connection_dis01_6.Open(); SqlReader_OOME = SqlCommand_OOME.ExecuteReader(); //<<<<<<<<ERROR HERE<<<<<<<<while (SqlReader_OOME.Read()) {//Manipulate Data... } SqlReader_OOME.Close();if (Connection_dis01_6.State == ConnectionState.Open) Connection_dis01_6.Close();

I have the message show up in this section...

if (Connection_dis01_5.State == ConnectionState.Closed) Connection_dis01_5.Open(); dis01DataAdapter.Fill(TempSet2); //<<<<<<<<ERROR HERE<<<<<<<<if (Connection_dis01_5.State == ConnectionState.Open) Connection_dis01_5.Close();
As you can see, I have multiple connecions and one connection is not used twice. I can easily recreate the message by just opening up multiple instances of this page. Thanks! I hope this code helps...the sections are really random and could show up anywhere a connection is used. Could it be a SQL Server setting I need to change?|||Here are my connections...
//Setting up Connections to dis01public static SqlConnection Connection_dis01_1 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_2 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_3 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_4 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_5 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_6 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_7 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_8 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_9 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_10 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_11 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_dis01_12 =new SqlConnection("CONNECTION_STRING_HERE");//Setting up Connections to ICCPpublic static SqlConnection Connection_ICCP_1 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_ICCP_2 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_ICCP_3 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_ICCP_4 =new SqlConnection("CONNECTION_STRING_HERE");//Setting up Connections to SCADApublic static SqlConnection Connection_SCADA_1 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_SCADA_2 =new SqlConnection("CONNECTION_STRING_HERE");public static SqlConnection Connection_SCADA_3 =new SqlConnection("CONNECTION_STRING_HERE");
|||

it is not good idea to create connections and keeping them all the time, modify your code to create connectiuon every time you would like to use it. .Net will take care about pooling so you connections if connection string is the same will be available very fast.

SqlConnection Connection_dis01_1 =new SqlConnection("CONNECTION_STRING_HERE");SqlCommand SqlCommand_ACK =new SqlCommand("SQL_STATEMENT", Connection_dis01_1);
//     try
      Connection_dis01_8.Open();
       SqlCommand_ACK.ExecuteNonQuery(); //<<<<<<<<ERROR HERE<<<<<<<<
// finally
          Connection_dis01_1.Close();
//end try
put your code in try catch finally and close connecion in finally part (I use VB so you have to writy your trty catsh yourself)
THis way your connection will be always closed and you save some server memory and time also.

No comments:

Post a Comment