Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SqlDataReader - Can't .Read() -Object reference not set to an instance

Status
Not open for further replies.

HFloyd

Programmer
Jun 5, 2002
71
US
Hello,

I have been getting this common error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

When attempting to read from an SqlDataReader object. (Note, I am using MS Data Application Blocks)
Here is my code:
Code:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Text
Imports Microsoft.ApplicationBlocks.Data

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim dbCnn As SqlConnection
        Dim strCnn As String
        Dim strSQL As String
        Dim strCurrSectionID As String
        Dim iCurrentSectionID As Integer
        Dim dsReader As SqlDataReader

        'Database connection
        strCnn = ConfigurationSettings.AppSettings("connectionstring")
        dbCnn = New SqlConnection(ConfigurationSettings.AppSettings("connectionstring"))
        dbCnn.Open()

        'Get current SectionID
        strCurrSectionID = Request.QueryString("SectionID")
        iCurrentSectionID = strCurrSectionID

        'Get dataset of sub-links
        strSQL = "sqrySection_Selected"
        dsReader = SqlHelper.ExecuteReader(strCnn, CommandType.StoredProcedure, strSQL, New SqlParameter("@SectionID", iCurrentSectionID))

        While dsReader.Read()
            Me.lblSectionName.Text = dsReader("SectionName")
            Me.lblDescription.Text = dsReader("SecLongDescript")
        End While

        dsReader.Close()
        dbCnn.Close()
    End Sub

The error is occuring on the line
Code:
While dsReader.Read()

I haven't found any references to this problem, and I have no idea where it's breaking down. The SP works fine if I call it from the SQL Query Analyzer.

Any ideas appreciated.

Thanks,

Heather

[yinyang] Floyd Innovations [yinyang]
 
I noticed you're passing strCnn to ExecuteReader when you already have an open connection. That looks a little fishy.

Also, you may want to make sure that the columns used in:

dsReader("SectionName")
dsReader("SecLongDescript")

actually exist.

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
BoulderBum,

Thanks for the try. Getting rid of "dbCnn.Open()" doesn't change anything.

I'm sure the columns "SectionName" and "SecLongDescript" exist, and even if they didn't, the error is appearing before they are referenced.

Heather



[yinyang] Floyd Innovations [yinyang]
 
Ah ha!

I figured out that the problem was in SQL Server, the stored procedure was not marked as "ASPNET" with "EXEC" permission.

Unfortunately, whereas if you go through the motions of cmd.executereader, you get an error message about permissions in SQL SERVER, but if you are using the MS Data Application Blocks, you don't get the same specific error message, since the SQLHelper code just doesn't return an object if it fails.

Heather

[yinyang] Floyd Innovations [yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top