EDN Admin
Well-known member
I have a doubt on the way I am handling the database connection objects. I am opening the database connection in the class, using it for queries and closing it by closing and disposing the database related objects. Time to time, I am getting following timout
related error message and I am suspecting that my Db connections are not properly getting closed.
this is the error message:
<span style="font-family:Times New Roman,serif; font-size:12pt System.InvalidOperationException:<br/>
Timeout expired. The timeout period elapsed prior to obtaining a connection<br/>
from the pool. This may have occurred because all pooled connections were in<br/>
use and max pool size was reached. at
Below is my code
I am opening the database in a class in following method
<pre class="prettyprint public static SqlConnection OpenSQLDatabase()
{
SqlConnection mySqlConnection = new SqlConnection(Connection_String);
try
{
mySqlConnection.Open();
return mySqlConnection;
}
catch (Exception e)
{
Exception_Message = e.ToString();
mySqlConnection.Dispose();
return mySqlConnection;
}
}[/code]
then using following method to get records from queries
<pre class="prettyprint public static SqlDataReader GetRecordsFromReader(String Sqlscript)
{
SqlConnection myconn = OpenSQLDatabase();
SqlDataReader myReader;
if (myconn.State.ToString() == "Open")
{
try
{
SqlCommand mycommand = new SqlCommand(Sqlscript, myconn);
myReader = mycommand.ExecuteReader();
return myReader;
}
catch (Exception e)
{
Exception_Message = e.ToString();
myReader = null;
myconn.Close();
myconn.Dispose();
return myReader;
}
}
else
{
Exception_Message = "Database Connection Error";
myReader = null;
myconn.Close();
myconn.Dispose();
return myReader;
}
}[/code]
Above methods are in the Class
Below shows how I am using them in the general code
<pre class="prettyprint SqlStr = "Select * From tblSoundContacts";
SqlDataReader drContacts = clsDatabase.GetRecordsFromReader(SqlStr);
if (drContacts.HasRows == true)
{
( RSContactFax = drContacts["Details"].ToString();
}
drContacts.Close();
drContacts.Dispose();
}
[/code]
I am closing the DataReader and disposing it whenever i use them. Is this the correct way to close the opened database connection? Please help me on this. I am struggling to find the cause for the error message
Thank you,
Raj
<hr class="sig Dreaming a world without any war in anywhere
View the full article
related error message and I am suspecting that my Db connections are not properly getting closed.
this is the error message:
<span style="font-family:Times New Roman,serif; font-size:12pt System.InvalidOperationException:<br/>
Timeout expired. The timeout period elapsed prior to obtaining a connection<br/>
from the pool. This may have occurred because all pooled connections were in<br/>
use and max pool size was reached. at
Below is my code
I am opening the database in a class in following method
<pre class="prettyprint public static SqlConnection OpenSQLDatabase()
{
SqlConnection mySqlConnection = new SqlConnection(Connection_String);
try
{
mySqlConnection.Open();
return mySqlConnection;
}
catch (Exception e)
{
Exception_Message = e.ToString();
mySqlConnection.Dispose();
return mySqlConnection;
}
}[/code]
then using following method to get records from queries
<pre class="prettyprint public static SqlDataReader GetRecordsFromReader(String Sqlscript)
{
SqlConnection myconn = OpenSQLDatabase();
SqlDataReader myReader;
if (myconn.State.ToString() == "Open")
{
try
{
SqlCommand mycommand = new SqlCommand(Sqlscript, myconn);
myReader = mycommand.ExecuteReader();
return myReader;
}
catch (Exception e)
{
Exception_Message = e.ToString();
myReader = null;
myconn.Close();
myconn.Dispose();
return myReader;
}
}
else
{
Exception_Message = "Database Connection Error";
myReader = null;
myconn.Close();
myconn.Dispose();
return myReader;
}
}[/code]
Above methods are in the Class
Below shows how I am using them in the general code
<pre class="prettyprint SqlStr = "Select * From tblSoundContacts";
SqlDataReader drContacts = clsDatabase.GetRecordsFromReader(SqlStr);
if (drContacts.HasRows == true)
{
( RSContactFax = drContacts["Details"].ToString();
}
drContacts.Close();
drContacts.Dispose();
}
[/code]
I am closing the DataReader and disposing it whenever i use them. Is this the correct way to close the opened database connection? Please help me on this. I am struggling to find the cause for the error message
Thank you,
Raj
<hr class="sig Dreaming a world without any war in anywhere
View the full article