System.NullReferenceException using DataReader in ASP.NET 4 (VB)

Discussion in 'Site Programming, Development and Design' started by mwmiller78, Jan 31, 2014.

  1. The code below works perfect in my test environment and on the Winhost (Windows/IIS7/.NET 4.0) server...

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                ID = 1
                ReadNext()
            End If
        End Sub 
    
        Private Sub ReadNext()
            Me.ResetDataReader()
            If CType(Session.Item("reader"), Data.SqlClient.SqlDataReader).Read() Then
                If Not CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("Team") Is DBNull.Value Then
                    lblTeam.Text = CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("Team").ToString
                Else
                    lblTeam.Text = ""
                End If       
            End If
        End Sub
    
        Private Sub ResetDataReader()
            Dim mySelectQueryReader As String = "SELECT * FROM tblTeams where ID like '" & ID & "'"
            Dim myConnectionReader As New Data.SqlClient.SqlConnection(myConStringReader)
            myCommandReader = New Data.SqlClient.SqlCommand(mySelectQueryReader, myConnectionReader)
            myConnectionReader.Open()
            myReader = myCommandReader.ExecuteReader
            Session.Item("reader") = myReader
        End Sub
    This code works perfect in my test environment, but not on the server

    Code:
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                ID = 1
                ReadNext()
            End If
        End Sub 
    
        Private Sub ReadNext()
            Me.ResetDataReader()
            If CType(Session.Item("reader"), Data.SqlClient.SqlDataReader).Read() Then
                If Not CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("Team") Is DBNull.Value Then
                    lblTeam.Text = CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("Team").ToString
                Else
                    lblTeam.Text = ""
                End If     
                If Not CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("AgeGroup") Is DBNull.Value Then
                    lblAge.Text = CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("AgeGroup").ToString
                Else
                    lblAge.Text = ""
                End If 
            End If
        End Sub
    
        Private Sub ResetDataReader()
            Dim mySelectQueryReader As String = "SELECT * FROM tblTeams where ID like '" & ID & "'"
            Dim myConnectionReader As New Data.SqlClient.SqlConnection(myConStringReader)
            myCommandReader = New Data.SqlClient.SqlCommand(mySelectQueryReader, myConnectionReader)
            myConnectionReader.Open()
            myReader = myCommandReader.ExecuteReader
            Session.Item("reader") = myReader
        End Sub
    Notice the only difference is the addition of

    Code:
    If Not CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("AgeGroup") Is DBNull.Value Then
    lblAge.Text = CType(Session.Item("reader"), Data.SqlClient.SqlDataReader)("AgeGroup").ToString
    Else
    lblAge.Text = ""
    End If 
    Again, when testing, this works great. As soon as I run it on my server, I get

    Server Error in '/' Application.

    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.

    Source Error:

    An unhandled exception was generated during the execution of the current web request.Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
    _MyProject.teampage.ReadNext() +689
    _MyProject.teampage.Page_Load(Object sender, EventArgs e) +136
    System.Web.UI.Control.OnLoad(EventArgs e) +91
    System.Web.UI.Control.LoadRecursive() +74
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1016

    Has anyone else experienced this? I've tried several variations of this all which work during testing but all fail on the Winhost server. I'm assuming it's losing the session data somehow after the first value is set to a label but I don't have a clue why. I've used this same code dozens of time (many on other Winhost sites) without issue. Any info would be appreciated.
     
  2. Have you checked your connection strings and made sure that they are all point to live databases?

    Also make sure all SQL Express (or other development db's) connection strings are commented out and/or removed, and that your code is not naming them in your connections from your code files.

    Lastly, you might need to insert another connection string named "LocalSqlServer" and see if that resolves your error.

    http://support.Winhost.com/KB/a813/parser-error-message-connection-name-localsqlserver.aspx
     
    Last edited by a moderator: Oct 14, 2015
    ComputerMan and Michael like this.

Share This Page