views:

24

answers:

1

Our application uses a custom DataAccessLayer class almost exclusively, and within that we do use Data Access Application Block (currently version 2). We are getting the infamous "GetOrdinal" error sporadically. We are not using out-of-method connections. We are using DAAB version 2. Below is a typical example of our DAL methods:

Public Function MyDALMethod(ByVal Param1 As Integer, ByVal Param2 As Integer) As System.Data.IDataReader

        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = "usp_MyProcedure"
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        db.AddInParameter(dbCommand, "Param1", DbType.Int32, MyParam1)
        db.AddInParameter(dbCommand, "Param2", DbType.Int32, MyParam2)

        Return db.ExecuteReader(dbCommand)

End Function

In our code we just instantiate a DAL var and call the desired method. After using the DataReader, the referencing code will close, dispose, and set the reader to nothing. However, nothing is done with the reference to the DAL. I've wondered if this is part of our problem. A typical method would use our DAL like this:

Dim DAL as New DataAccessLayer
Dim dbReader as system.data.idatareader

dbreader = DAL.MyDALMethod(parm1, parm2)

While dbreader.read
    i = dbreader.items("column1")
    j = dbreader.items("column2")
End While

dbreader.close()
dbreader.dispose()
dbreader = nothing

My main question is should these DAL references be disposed somehow? It's a custom class written in VB.NET, so it doesn't implement IDisposable so I'm not sure if there's anything to be done or not, but we do have errors and issues (like the GetOrdinal problem) which seem to be load-related, and I'm wondering if this is part of the problem.

+1  A: 

If the DAL holds at least one member variable that is Disposable (implements IDisposable), then it too should implement IDisposable. This would then indicate to DAL's client that Dispose() should be called. The DAL's Dispose() would then call the member variable(s) Dispose().

That is the general guidance for implementing IDisposable.

BTW - there is no need to dbreader = nothing - it achieves nothing. See Eric Lippert's post

thehowler