473,246 Members | 1,584 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,246 software developers and data experts.

Close connection problem

Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecified error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.OleDb.OleDbDataReader DataReaderFunc(string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection();

objConn.Close();
objConn.ConnectionString = GetConnection();
OleDbCommand objComm = new OleDbCommand(SQLSelect, objConn);
objConn.Open();
objDataRead = objComm.ExecuteReader(CommandBehavior.CloseConnect ion);

Console.WriteLine (objConn.State);
//objConn.Dispose();
return objDataRead;
}

Dec 29 '05 #1
5 2023
Your Connection remains open, alright. Worse than that, its scoped inside a
method, so when your method exits, you lose any handle on it you had while
the method was executing.

A DataReader requires a persistent Connection. For the entire time you use a
DataReader, its Connection must remain open. Therefore, you need to declare
your Connection outside of the method, so that when you are finished with
the DataReader, you can close the Connection.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecified error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.OleDb.OleDbDataReader DataReaderFunc(string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection();

objConn.Close();
objConn.ConnectionString = GetConnection();
OleDbCommand objComm = new OleDbCommand(SQLSelect, objConn);
objConn.Open();
objDataRead = objComm.ExecuteReader(CommandBehavior.CloseConnect ion);

Console.WriteLine (objConn.State);
//objConn.Dispose();
return objDataRead;
}

Dec 29 '05 #2
He is passing in ConnectionBehavior.CloseConnection. So as long as the data
reader is closed, so will the connection, and that will avoid a connection
leak. This means that the connection is fine declared where it is.

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:es*************@TK2MSFTNGP14.phx.gbl...
Your Connection remains open, alright. Worse than that, its scoped inside
a method, so when your method exits, you lose any handle on it you had
while the method was executing.

A DataReader requires a persistent Connection. For the entire time you use
a DataReader, its Connection must remain open. Therefore, you need to
declare your Connection outside of the method, so that when you are
finished with the DataReader, you can close the Connection.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecified error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.OleDb.OleDbDataReader DataReaderFunc(string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection();

objConn.Close();
objConn.ConnectionString = GetConnection();
OleDbCommand objComm = new OleDbCommand(SQLSelect, objConn);
objConn.Open();
objDataRead = objComm.ExecuteReader(CommandBehavior.CloseConnect ion);

Console.WriteLine (objConn.State);
//objConn.Dispose();
return objDataRead;
}


Dec 29 '05 #3
Ah, yes. Sorry, overlooked it.

Well, the only thing left is whether or not he's closing the DataReader
outside the method somewhere.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Marina" <so*****@nospam.com> wrote in message
news:uu*************@TK2MSFTNGP10.phx.gbl...
He is passing in ConnectionBehavior.CloseConnection. So as long as the
data reader is closed, so will the connection, and that will avoid a
connection leak. This means that the connection is fine declared where it
is.

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:es*************@TK2MSFTNGP14.phx.gbl...
Your Connection remains open, alright. Worse than that, its scoped inside
a method, so when your method exits, you lose any handle on it you had
while the method was executing.

A DataReader requires a persistent Connection. For the entire time you
use a DataReader, its Connection must remain open. Therefore, you need to
declare your Connection outside of the method, so that when you are
finished with the DataReader, you can close the Connection.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecified error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.OleDb.OleDbDataReader DataReaderFunc(string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection();

objConn.Close();
objConn.ConnectionString = GetConnection();
OleDbCommand objComm = new OleDbCommand(SQLSelect, objConn);
objConn.Open();
objDataRead = objComm.ExecuteReader(CommandBehavior.CloseConnect ion);

Console.WriteLine (objConn.State);
//objConn.Dispose();
return objDataRead;
}



Dec 29 '05 #4
In my experience, when there is a connection leak and there is a problem
obtaining a new connection, the error message is usually something that
mentions timing out getting a new connection from the connection pool.

Also, database errors will usually have more information other then
'unspecified error'. Are you sure this is the complete error message in the
exception being thrown?

To avoid connection leaks in this type of code, you should make sure you
close the datareader in whatever code is calling this function, to make sure
that the connection the data reader is using is closed.

"Varangian" <of****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello there people,

I'm having some kind of problem. I have a function that returns a
datareader. At some point using the application I get an error
"Unspecified error" (ssssoooo helpful) :). I think I know the problem.
My Connection remains open.

Is there a way I can do to close the Connection. below is the code.
Thank you very much as always

public System.Data.OleDb.OleDbDataReader DataReaderFunc(string
SQLSelect)
{
OleDbDataReader objDataRead = null;
OleDbConnection objConn = new OleDbConnection();

objConn.Close();
objConn.ConnectionString = GetConnection();
OleDbCommand objComm = new OleDbCommand(SQLSelect, objConn);
objConn.Open();
objDataRead = objComm.ExecuteReader(CommandBehavior.CloseConnect ion);

Console.WriteLine (objConn.State);
//objConn.Dispose();
return objDataRead;
}

Dec 29 '05 #5
Unspecified error
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.Data.OleDb.OleDbException: Unspecified error
-

Line 54: OleDbCommand objComm = new OleDbCommand(SQLSelect,
objConn);
Line 55:
Line 56: objConn.Open();
Line 57: objDataRead =
objComm.ExecuteReader(System.Data.CommandBehavior. CloseConnection);

This is the error I'm getting. with the
ExecuteFunction(CloseConnection) the application lasts a bit more..
however there's still something wrong and the error points into the
objConn.Open();

:) Thanks for the Help

Dec 30 '05 #6

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

Similar topics

3
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a...
3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
6
by: No_Excuses | last post by:
All, I am interested in reading the text of a web page and parsing it. After searching on this newgroup I decided to use the following: ******************************* START OF CODE...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
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...
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...
0
by: bonita | last post by:
In my ASP.NET page, I have 2 checkboxes for users to choose which crystal report they want to display. These two reports use different tables. If report1 has been choosen and displayed in the...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
8
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.