Unhandled Exception

R

RAJ

Hi,
While connecting to a database and using ExecuteReader in my
application, I am getting the following error:

**************************************************
An Unhandled Exception of type 'System.InvalidOperation Exception'
occurred in System.Data.dll
ExecuteReader: Connection Property has not been initialized.
**************************************************
I checked the connection strings and it's correct.While debugging I
have seen the connection property details.(On Mouse Pointer).
But the connection is not opening in response to Connection.Open()
statement before invoking the ExecuteReader.
Please let me know if any body knows the resolution or suggestions.
******************************
Here is my code:
Dim strSQL As String

Dim cmdDeath As SqlClient.SqlCommand
Dim drDeath As SqlClient.SqlDataReader

strSQL = "select max(batchid) as MaxBatchID from Tablename"
'''Declared g_conDeath as public with values from strConn
cmdDeath = New SqlClient.SqlCommand(strSQL, g_conDeath)
drDeath = cmdDeath.ExecuteReader '''getting error here
**********************************************
Thanks.
 
W

William Ryan eMVP

Where are you opening the connection? Also, as a rule, since so many
different things can go wrong with Connection.open not related to your code
(an unplugged network cable, server being down etc), it's not a bad idea to
wrap it in it's own try catch block b/c you aren't going to go any further
if you can't open a connection.

If you don't have an open connection you'll get this error and from the
looks of the code below, that's the most likely culprit.

HTH,

Bill
RAJ said:
Hi,
While connecting to a database and using ExecuteReader in my
application, I am getting the following error:

**************************************************
An Unhandled Exception of type 'System.InvalidOperation Exception'
occurred in System.Data.dll
ExecuteReader: Connection Property has not been initialized.
**************************************************
I checked the connection strings and it's correct.While debugging I
have seen the connection property details.(On Mouse Pointer).
But the connection is not opening in response to Connection.Open()
statement before invoking the ExecuteReader.
Please let me know if any body knows the resolution or suggestions.
******************************
Here is my code:
Dim strSQL As String

Dim cmdDeath As SqlClient.SqlCommand
Dim drDeath As SqlClient.SqlDataReader

strSQL = "select max(batchid) as MaxBatchID from Tablename"
'''Declared g_conDeath as public with values from strConn
cmdDeath = New SqlClient.SqlCommand(strSQL, g_conDeath)
drDeath = cmdDeath.ExecuteReader '''getting error here
**********************************************
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top