473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to close sqlconnection

I have a method that gets sqldatareader as shown below.. my question is how
do i close the sqlconnection (objConn) object within this method?

If i put the objconn.Close(); after the return then i get "Unreachable code
detected" and if i close it before the return i get "Invalid attempt to
FieldCount when reader is closed."

When i refresh the page .. lot of sqlconnection is created.. and then i get
this error message :
Timeout expired. The timeout period elapsed prior to obtaining a connection
from the pool. This may have occurred because all pooled connections were in
use and max pool size was reached.

Also instead of adding this bit of code
[(ConfigurationSettings.AppSettings["DSN"]);
SqlCommand selectCmd = new SqlCommand("sp_DMListFiles",objConn)]
in every method.. how can i put it somewhere and reuse it in every methods??

Many thanks in advance.

public SqlDataReader DMListFiles(int DepID, int FolderID)
{
SqlConnection objConn = new
SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
SqlCommand selectCmd = new SqlCommand("sp_DMListFiles",objConn);
selectCmd.Parameters.Add("@DepID",DepID);
selectCmd.Parameters.Add("@FolderID",FolderID);
selectCmd.CommandType = CommandType.StoredProcedure;
objConn.Open();
SqlDataReader dr = selectCmd.ExecuteReader();
return dr;
}
Jul 21 '05 #1
1 4893
Hi,

you can't close it in the same method after you've returned it. It lefts
that responsibility to the caller of this method. Least you can do is to
call ExecuteReader method with CloseConnection argument that is:

SqlDataReader dr = selectCmd.ExecuteReader(CommandBehavior.CloseConne ction);
return dr;

which would force the connection to be closed when Datareader is closed, but
still the caller *must* call Close/Dispose for the data reader instance

However, there is a way around it, simplest being that start using data
tables, but you can check a few articles about these issues how you can
manage with data readers in a case like this.

Using Delegates With Data Readers to Control DAL Responsibility
http://aspalliance.com/526

..NET Data Access Performance Comparison
http://aspalliance.com/626

--
Teemu Keiski
ASP.NET MVP, Finland
"huzz" <hu**@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
I have a method that gets sqldatareader as shown below.. my question is how
do i close the sqlconnection (objConn) object within this method?

If i put the objconn.Close(); after the return then i get "Unreachable
code
detected" and if i close it before the return i get "Invalid attempt to
FieldCount when reader is closed."

When i refresh the page .. lot of sqlconnection is created.. and then i
get
this error message :
Timeout expired. The timeout period elapsed prior to obtaining a
connection
from the pool. This may have occurred because all pooled connections were
in
use and max pool size was reached.

Also instead of adding this bit of code
[(ConfigurationSettings.AppSettings["DSN"]);
SqlCommand selectCmd = new
SqlCommand("sp_DMListFiles",objConn)]
in every method.. how can i put it somewhere and reuse it in every
methods??

Many thanks in advance.

public SqlDataReader DMListFiles(int DepID, int FolderID)
{
SqlConnection objConn = new
SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
SqlCommand selectCmd = new
SqlCommand("sp_DMListFiles",objConn);
selectCmd.Parameters.Add("@DepID",DepID);
selectCmd.Parameters.Add("@FolderID",FolderID);
selectCmd.CommandType = CommandType.StoredProcedure;
objConn.Open();
SqlDataReader dr = selectCmd.ExecuteReader();
return dr;
}

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Pierson C | last post by:
I am developing on a website that is utilizing SQL Server 2000. Shortly after deploying the site, we began having timeout issues due to the max connections. 1st instinct was to diligently tidy...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
3
by: FC | last post by:
Hello All: I am getting the following error: Compiler Error Message: CS0246: The type or namespace name 'conn' could not be found (are you missing a using directive or an assembly reference?) ...
3
by: Woody Splawn | last post by:
I have some code which I will show below that opens a SQLDataReader, gets some information and then closes. My question is, what is the proper way to close? Do I close the reader first and then...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
1
by: huzz | last post by:
I have a method that gets sqldatareader as shown below.. my question is how do i close the sqlconnection (objConn) object within this method? If i put the objconn.Close(); after the return then i...
1
by: John Bailo | last post by:
using ( SqlCommand cmd = new SqlCommand("select * sample"), new SqlConnection( sqlDsn ) ) ) { SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable();...
6
by: mavrick_101 | last post by:
do I need to explicitly do a Connection.Dispose() too? Thnx
8
by: Imran Aziz | last post by:
Hello All, Like in C++ I tried to use constructor to open a database connection and distructor to close the database connection, it now turns out that one cannot create distrutors in C# classes. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.