VB.NET - Cannot open an ADODB connection - Asked By Avishek Prasad on 19-Mar-12 10:28 AM

Set pbnut_cn = CreateObject("ADODB.Connection")
 pbnut_cn.Open strConn   // code gets stuck at this point

The above connection is established in a sub routine which is called from its parent sub routine. And there is a connection already opened in the parent sub.
By trying TO CHECK THE ERROR I could only find out that the connection object is closed and still I am trying to open it. But, am clueless on how the connection object can already remain open 'coz it has nowhere else been declared.
D Company replied to Avishek Prasad on 19-Mar-12 11:43 AM
How u have opened the connection in parent sub routine? check the way. second thing close the connection instance once the process is done, now in the mention code snippet check the connection state before opening it .and if its state is closed then open it. also check the ConnectionTimeout property.

perform this check in parent sub routine and see if there is an instance of the connection is still active.like this
MsgBox "Cnxn1 state: " & GetState(Cnxn1.State)

Hope this helps
[)ia6l0 iii replied to Avishek Prasad on 19-Mar-12 12:39 PM
In any of the sub-routines, where you create the connection this is the pattern that you need to follow.

Function SomeFunction(parameter 1)
'Create a connection
Dim mConnection
Set pbnut_cn = CreateObject("ADODB.Connection")
'Open the connection
pbnut_cn.Open strConn   
'Execute the statements using the connection object.
pbnut_cn.Execute "statement"
'Dispose the connection object
Set pbnut_cn= Nothing
End Function

Hope this helps.
Mitesh Darji replied to Avishek Prasad on 19-Mar-12 01:37 PM
1) check Provider in your connection string
2) check connection state object State method, it will return connection state (1=open,0=closed)

check below example

Option Explicit
Dim strConnection, conn, rs, strSQL

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath(".\db\db.mdb") & ";User Id=admin;Password=;"

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strConnection

Set rs = Server.CreateObject("ADODB.recordset")
strSQL = "SELECT * FROM myTable"
rs.open strSQL, conn, 3,3

   Response.Write(rs("myField") & "<br/>")

Set rs = Nothing

Set conn = Nothing 
kalpana aparnathi replied to Avishek Prasad on 19-Mar-12 02:09 PM

Try below example:

Dim strConn As String = "server='servername'; user id='sa'; password='admin'; database='databasename';"
Dim MyConn_member As New SqlConnection(strConn)
'start transaction
Dim MyTrans As SqlTransaction = MyConn_member.BeginTransaction
'first statement to run...
Dim MySql_member As String = "<--sql query string-->"
Dim cmd_member As New SqlCommand(MySql_member, MyConn_member, MyTrans)
cmd_member.Parameters.Add(New SqlParameter("pass the para meter))

Avishek Prasad replied to D Company on 19-Mar-12 03:08 PM
Hey D,

I had already tried putting a check for the connection state. And the point where the connection object is made, I had checked for the state and it was closed. I tried to open the connection after that but no luck.
below is the code flow :

'declare a connection 
'open a connection

'record set operation to execute a SQL stored proc using the connection established above
'record set eof check operations and all

'Call to SUB B

'closing record set 
'closing connection and setting it to nothing



'declare a connection  // connection is closed here
'open a connection // trying to open it but is not working(this is working in SUB A). code freezes here

'record set operation to execute a SQL stored proc using the connection established above
'record set eof check operations and all

'closing record set 
'closing connection and setting it to nothing 


I have no clue as to what is happening 'coz the same code is working fine when I just extract the above code snippet and run it as a separate script.

Need your help.. thanks

D Company replied to Avishek Prasad on 20-Mar-12 12:46 AM
In the sub A section use dispose statement like this

Set pbnut_cn= Nothing


Somesh Yadav replied to Avishek Prasad on 20-Mar-12 01:35 AM
You didn't assign a connection object or connection string to your recordset rstrolley.


rsTrolley.ActiveConnection = cn



rsTrolley.Open strSQL, cn

Here is some sample code:


Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset

Set cn = new ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Password=TestPass;Data Source=C:\Test.mdb;Persist Security Info=False;Exclusive=0"

set rs = new ADODB.recordset
rs.ActiveConnection = cn
rs.Open "Select Distinct family From Products"

Set rs=Nothing
Set cn = Nothing

Reena Jain replied to Avishek Prasad on 20-Mar-12 02:51 AM

In many earlier applications, the tendency was to open a connection when you start the application and not close the connection until the application terminates. It is an expensive and time-consuming operation to open and close a database connection. Most databases have a limit on the number of concurrent connections that they allow.

For example: each connection consumes a certain amount of resources on the database server and these resources are not infinite. Most modern OLE DB providers (including SQL Server provider) implement connection pooling. If you create database connections, they are held in a pool. When you want a connection for an application, the OLE DB provider extracts the next available connection from the pool. When your application closes the connection, it returns to the pool and makes itself available for the next application that wants a connection.

This means that opening and closing a database connection is no longer an expensive operation. If you close a connection, it does not mean you disconnect from the database. It just returns the connection to the pool. If you open a connection, it means it's simply a matter of obtaining an already open connection from the pool. It's recommended in many ADO.NET books not to keep the connections longer than you need to. Therefore, you should:

  •     Open a connection when you need it, and
  •     Close it as soon as you have finished with it.
Hope this will help you
Jitendra Faye replied to Avishek Prasad on 20-Mar-12 03:23 AM
First check the scope of that connection variable, because if it is global then you can access it any where so after opening connection, connection state will be opened till you close it.
Avishek Prasad replied to Somesh Yadav on 20-Mar-12 03:24 AM
Hey Somesh,

I have already assigned the connection and the SQL string while opening the recordset. It goes like as below:

Set pbimport_cn = CreateObject("ADODB.Connection")

If pbimport_cn.State = 1 then
  WriteToLog("**** Conn is open ****")
Else If pbimport_cn.State = 0 then
  WriteToLog("**** Conn is closed ****")
  pbimport_cn.Open strConn   // opening the connection here but it fails to open, strConn = "Provider=SQLOLEDB;Data Source=" datasource here ";" & "Initial Catalog=" catalog here ";Integrated Security=SSPI;"   
    WriteToLog("**** Conn is open now ****")
   End If 
End If

Set pb_rs = CreateObject("ADODB.Recordset")

 strSQL = "Executing proc here" 
 pb_rs.open strSQL, pbimport_cn, 3, 3        // adOpenStatic = 3, adLockOptimistic = 3

' record set operations are performed here (calling SUB B from here in SUB A) -- this is the only difference

Set pb_rs = nothing

Set pbimport_cn = nothing

The above code is in SUB B.
Similar code is in SUB A as well but the difference is that the connection is still open there before calling SUB B from there.
Rest all(like closing the connection or recordset and disposing them) are same in both the sub routines.

Please advice. Thanks
Avishek Prasad replied to Jitendra Faye on 21-Mar-12 02:59 AM
Hey Everyone,

I have noticed a very peculier behavior while running the vb script in two different ways.
One way is through the command prompt and another way is by running the script as a scheduled task.

When it runs via command prompt, the connection gets opened perfectly.But when run as a scheduled task, the code gets stuck while opening the connection.

Could anyone please explain this behavior ?