Home
 Index > ASP.NET Web Applications > Object Reference not Set to an Instance of an O...

ASP.NET Web Applications:  Databinding , Datagrid , IO , GridView , FormView , DetailsView

Object Reference not Set to an Instance of an Object

The most common error in .NET applications can occur in a variety of scenarios.

Added on 25 Jun 2008

Scenarios:

Scenario Summary:
SqlDataReader error.
Scenario Details:

The error occurs when reading data from a SqlDataReader, such as in the following code:

    Private Sub ExecuteProcedure(ByVal myParameter As Integer)
        
Dim dr As SqlDataReader = ExecuteReader(conString, "MyStoredProcedure", myParameter)
        
If dr.Read Then
            lblMessage.Text = dr("Message")
            lblMessage.Visible = 
True
            dr.Close()
        
End If
    End Sub
Added on 25 Jun 2008

Solution Summary:
Convert values returned by the datareader to string.
Solution Details:

If a value returned by the datareader is NULL, the object will be a null reference.  Convert the value to a string if it's possible that the value might be nothing:

lblMessage.Text = Convert.ToString(dr("Message"))
Was this solution useful? Yes No Added on 25 Jun 2008
Rating: 

Solution Summary:
The reader itself is nothing because the stored procedure does not return values or because there were errors in the query or stored procedure.
Solution Details:

The SqlDataReader cannot be read because there was an error in the query or stored procedure.  Open SQL Query Analyzer and execute the query providing the same parameter values provided by the SqlDataReader.  If an error occurs, modify the procedure or query as needed.

If no error occurred, check that values are being returned.  If there are no return values, the SqlDataReader cannot be read.  Either return values or use a SqlDataAdapter instead of a reader.

Was this solution useful? Yes No Added on 25 Jun 2008
Rating: 

Copyright 2010 © E-Centric, Inc. | Terms of Use