the connection is already open (state=open)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
In an asp.net app I had used the following to create a connection in various
places...
dim cn as sqlconnection = new
sqlconnection(syetem.configuration.configurationsettings.appsettinga("DBConnectString"))

Aside from whether this is the securest place to store the string, all was
working well. I then decided to tidy things up and replace the above line
with a call to a shared function which simply had the above line in it, and
the function returned the connection object.
Ok I thought, but then I started to get the following error message..
the connection is already open (state=open)

Does anybody know why this might be the case? i can't see why one is
different from the other.

Thanks,
Andy
 
Hi Andy,

You are certainly trying to open the same connection again.
As Marina said, it is difficult to guess why..
 
Marina / Miha,

In looking at it again, it looks as though the function was returning an
already openned connection.. i.e.

Public Shared Function GetConnection() As System.Data.SqlClient.SqlConnection
Dim cn As SqlConnection = New
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("DBConnectionString"))
cn.Open()
GetConnection = cn
End Function

It looks lik that was the cause.

Many thanks.
Andy

Miha Markic said:
Hi Andy,

You are certainly trying to open the same connection again.
As Marina said, it is difficult to guess why..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Andy said:
Hi,
In an asp.net app I had used the following to create a connection in
various
places...
dim cn as sqlconnection = new
sqlconnection(syetem.configuration.configurationsettings.appsettinga("DBConnectString"))

Aside from whether this is the securest place to store the string, all was
working well. I then decided to tidy things up and replace the above line
with a call to a shared function which simply had the above line in it,
and
the function returned the connection object.
Ok I thought, but then I started to get the following error message..
the connection is already open (state=open)

Does anybody know why this might be the case? i can't see why one is
different from the other.

Thanks,
Andy
 
Back
Top