Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

OleDbDataReader Null REference

Hai,

I am Getting some query Result in a OLEDBDataReader. In one case I am using .read method, and in another case I am using that Reader as DataSource of a DataGrid,and do DataBind.
It giving expected result some time,and some other times,it is showing Error,Null Refernce,Object Reference not set to instance of a object.If refreshed again and again, It is showing the result.
What is the problem,How to overcome this?

Here is the code that populates the objDataReder.

Public Shared Function ShowResult(ByVal Qry As String) As Object
Try
If Not (objDataReader Is Nothing) Then
objDataReader.Close()
End If
selectCmd = New OleDbCommand(Qry, objCon)
'objCon is the connection object that is opened in session_OnStart.

objDataReader = selectCmd.ExecuteReader()
Return objDataReader
Catch
errorTracer()
End Try
End Function

Help in time will be grateful.

Regards
sindhuja
[986 byte] By [sindhuja] at [2007-11-11 7:21:33]
# 1 Re: OleDbDataReader Null REference
When you use the data reader to bind data on to a grid

Always check the datareader.hasrows method before you bind the data to the grid
eg
if(dr.HasRows)
{
then bind data to the grid
}
else
{
display an empty grid / no rows result message
}
Also beware of nulls in your column use the Isnull function in your stored procedure to handle
srinivas_s at 2007-11-11 21:49:18 >
# 2 Re: OleDbDataReader Null REference
I am using ASP.net and in that there is now property like HasRows for DataReader.
And the thing is, for the same set of result I am getting the correct display. Only at times (but frequently) It shows the NullReferenceException. Is there anything related to the DataBase connectivity.Will there be any queue in getting the select command being executed.
Pl give me reply if any idea...
sindhuja at 2007-11-11 21:50:11 >
# 3 Re: OleDbDataReader Null REference
No this is not anything to do with database connectivity ...

Do you bind the datareader directly to the grid?
Secondly is this datareader executed on post back and then loaded based on a user selection? Is it on the postback

Lastly post the code of the binding that happens with the grid on the webform.
srinivas_s at 2007-11-11 21:51:20 >