Hi,
I have a function which returns an sqldatreader, I know within the function it works, but when it returns to the calling function it says the reader is closed?
CALLING CODE
Public Sub PopulateSectors()
Dim odataaccess As CustomControls.DataOperations.DataAccess
Dim oDataReader As SqlDataReader
Try
oDataAccess = New CustomControls.DataOperations.DataAccess
oDataReader = oDataAccess.PopulateSectors
lstSectors.DataSource = oDataReader
lstSectors.DataBind()
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
Finally
odataaccess = Nothing
End Try
End Sub
CALLED CODE
Public Function PopulateSectors() As SqlDataReader
Dim oConnection As SqlConnection
Dim oCommmand As SqlCommand
Dim oDr As SqlDataReader
Try
'Set connection object
oConnection = New SqlConnection(ConfigurationManager.AppSettings("sqlCommunications"))
oConnection.Open()
oCommmand = New SqlCommand
oCommmand.Connection = oConnection
oCommmand.CommandType = CommandType.Text
oCommmand.CommandText = "select CompanyName, ContactID from [view_Merchants] order by CompanyName"
'oDr = oCommmand.ExecuteReader
oDr = oCommmand.ExecuteReader(CommandBehavior.CloseConnection)
' oDr.Read()
Return oDr
Catch exsql As SqlException
Throw New ApplicationException(exsql.Message, exsql)
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
Finally
If Not oDr Is Nothing Then oDr.Close() : oDr = Nothing
oCommmand = Nothing
If oConnection.State = ConnectionState.Open Then oConnection.Close() : oConnection = Nothing
End Try
End Function
Thanks
I have a function which returns an sqldatreader, I know within the function it works, but when it returns to the calling function it says the reader is closed?
CALLING CODE
Public Sub PopulateSectors()
Dim odataaccess As CustomControls.DataOperations.DataAccess
Dim oDataReader As SqlDataReader
Try
oDataAccess = New CustomControls.DataOperations.DataAccess
oDataReader = oDataAccess.PopulateSectors
lstSectors.DataSource = oDataReader
lstSectors.DataBind()
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
Finally
odataaccess = Nothing
End Try
End Sub
CALLED CODE
Public Function PopulateSectors() As SqlDataReader
Dim oConnection As SqlConnection
Dim oCommmand As SqlCommand
Dim oDr As SqlDataReader
Try
'Set connection object
oConnection = New SqlConnection(ConfigurationManager.AppSettings("sqlCommunications"))
oConnection.Open()
oCommmand = New SqlCommand
oCommmand.Connection = oConnection
oCommmand.CommandType = CommandType.Text
oCommmand.CommandText = "select CompanyName, ContactID from [view_Merchants] order by CompanyName"
'oDr = oCommmand.ExecuteReader
oDr = oCommmand.ExecuteReader(CommandBehavior.CloseConnection)
' oDr.Read()
Return oDr
Catch exsql As SqlException
Throw New ApplicationException(exsql.Message, exsql)
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
Finally
If Not oDr Is Nothing Then oDr.Close() : oDr = Nothing
oCommmand = Nothing
If oConnection.State = ConnectionState.Open Then oConnection.Close() : oConnection = Nothing
End Try
End Function
Thanks