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:
2
Answers
If you have a
DataTabl
e stored in theSession
variablebuyitems
then do not create aNew
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 theSession
variable.This code should solve the issue.