Wierd Occurrance

G

Guest

I have an intermittant problem that occurs a couple of times a week in a
handful of places in the code (the same places at different times). The
error is 'Object reference not set to an instance of an object'. After much
research (it's not my code), I've found that it is occuring when a datareader
is being used after being returned from a another method that queries an
Oracle database. Here is the method:

Public Function GetDataReader(ByVal strSelect As String) As
OleDbDataReader
Dim rc As Integer

Const FUNCTION_NAME As String = CLASS_NAME + ":SelectSQL"

Try
objConn = New OleDbConnection(strConnectionString)
objCmd = New OleDbCommand(strSelect, objConn)
objCmd.Connection.Open()
GetDataReader =
objCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As OleDbException
If oLogFile Is Nothing Then oLogFile = New clsLogFile()
rc = oLogFile.LogErrorMessage(FUNCTION_NAME, ex.ErrorCode,
ex.Message + " SQL Statement: |" + strSelect + "|")
GetDataReader = Nothing
Finally
objConn = Nothing
objCmd = Nothing
End Try
End Function

As I mentioned above, the error is occurring the first time the returned
datareader is used in the calling code. The first thing the calling code
does is check to see if the returned datareader is closed. When this
statement executes the datareader object does not exist. Here is an example
of the calling code:

oDatabase = New ICPT.clsDatabase(oSession.ApplicationConnectionString,
oSession.DatabaseType)

strSQL = "SELECT * FROM TransactionCodes"
rdrTranCode = oDatabase.GetDataReader(strSQL)

'verify the datareader is open and contains a record
If rdrTranCode.IsClosed = False Then '' bomb on this line
'do something
End If


I've checked the application log files and there is no indication that the
GetDataReader() method is trapping and logging an error. Any of you guys see
a problem with this code? It looks as if a database connection and
subsequent query are being done successfully (nothing in error logs), but
when the datareader is returned the reference to the datareader is lost.

I can tell you that most of the things this application does involve at
least a few calls to GetDataReader(), and sometimes there are quite a few
calls to perform a certain function. I've checked the Oracle log and trace
files and there is no indication of anything flaky.

Any ideas?

Thanks,
Todd
 
V

Val Mazur \(MVP\)

Hi Todd,

I think when you call objCmd = Nothing in a Finally section, you destroy not
just a command object variable but also associated with this class
DataReader. Try to remove it to see if it helps
 
G

Guest

If this were the problem, wouldn't this issue be occurring every time the
datareader object that is returned from GetDataReader() was used? This issue
occurs maybe once or twice a week, sometimes not at all for a week. The
users perform the same operations every day, so they hit the same pages
every day. If this were the issue I would think the issue would occur every
day, unless .Net GC has something to do with it?

I did some research on passing DataReader objects out of a method and this
seemed to be the consensus on how to do it without losing your database
connection. Could there be a bug/problem in .Net that is allowing GC to
dispose of these objects being passed out of methods like this?
 
G

Guest

The production machine is on 1.1. Not sure if any service packs have been
applied. Since this is an intermittant issue we haven't been able to
reproduce it as of yet.
 

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

Similar Threads


Top