skip to Main Content
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As New DataTable

        dt = CType(Session("buyitems"), DataTable)

        If (dt Is Nothing) Then
            Label5.Text = dt.Rows.Count.ToString()
        Else
            Label5.Text = "0"
        End If
    End Sub

    Protected Sub DataList1_ItemCommand(source As Object, e As DataListCommandEventArgs) Handles DataList1.ItemCommand

        Dim dlist As DropDownList = CType(e.Item.FindControl("DropDownList1"), DropDownList)
        Response.Redirect("AddToCart.aspx?id=" + e.CommandArgument.ToString() + "&quantity=" + dlist.SelectedItem.ToString)
    End Sub

I get the exception of System.NullreferenceException as "Object reference is not set to an instance of an object:

Exception Details

2

Answers


  1. If you have a DataTable stored in the Session variable buyitems then do not create a New one when you declare the local variable.

    I think you just reversed assignments in the If statement.

    It seems that there is not a DataTable in the Session variable.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As DataTable
        dt = CType(Session("buyitems"), DataTable)
        If dt Is Nothing Then
            Label5.Text = "0"
        Else
            Label5.Text = dt.Rows.Count.ToString()
        End If
    End Sub
    
    Login or Signup to reply.
  2. If (dt Is Nothing) Then
            Label5.Text = "0"
        Else
            Label5.Text = dt.Rows.Count.ToString()
        End If
    

    This code should solve the issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search