Weird issue with System.NullReferenceException

S

Shannon

I am having a very strange issue with my asp.net application. I have
created a datagrid which dynamically gets populated from a person
choosing a value from a dropdown list and entering a quantity into a
text box (a simple version of a shopping cart). When I run/debug the
web page using the localhost in the page URL
(http://localhost/mypage.aspx), the datagrid updates properly when a
command button is clicked and all is well. However, when I run the web
page using the machinename (http://mymachine/mypage.aspx) I recieve the
error "Object Reference not Set...System.NullReferenceException" when I
click the command button to update the datagrid with new values. Does
anyone have a solution as to why it works as localhost but does not as
machinename? And does anyone have a solution? Thanks in advance.
Shannon
 
S

Shannon

Here is the requested code behind.

Imports System.Data.SqlClient
Public Class merchandise
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lblNavBar As System.Web.UI.WebControls.Label
Protected WithEvents lblMerchandise As
System.Web.UI.WebControls.Label
Protected WithEvents merTXT1 As System.Web.UI.WebControls.Label
Protected WithEvents merTXT2 As System.Web.UI.WebControls.Label
Protected WithEvents ddProducts As
System.Web.UI.WebControls.DropDownList
Protected WithEvents txtQty As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents btnCart As
System.Web.UI.WebControls.LinkButton
Protected WithEvents dgCart As System.Web.UI.WebControls.DataGrid
Protected WithEvents lblTotal As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Public objDT As System.Data.DataTable
Public objDR As System.Data.DataRow

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim dbConn As New
SqlConnection(ConfigurationSettings.AppSettings("ConnectRWData"))

'retrieve the data from the database for the List Box
Content
Dim daMerchandise As New SqlDataAdapter("SELECT prodTYPE,
prodCOST, prodTYPE + ' - $' + CAST(prodCOST AS char(5)) AS prodLIST
FROM tblProducts", dbConn)

'fill the dataset
Dim dsmerchandise As New DataSet
daMerchandise.Fill(dsMerchandise, "tblProducts")

'fill the list box with the values.
ddProducts.DataSource = dsmerchandise.Tables("tblProducts")
ddProducts.DataTextField = "prodLIST"
ddProducts.DataValueField = "prodCOST"
ddProducts.DataBind()

'create the dynamic shopping cart table
makeCart()
End If
End Sub

Function makeCart()

objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1

objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
objDT.Columns.Add("Cost", GetType(Decimal))

Session("Cart") = objDT
End Function

Public Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
objDT = Session("Cart")

Dim Product = ddProducts.SelectedItem.Text

objDR = objDT.NewRow 'this is the line creating the error when
run under machinename
objDR("Quantity") = txtQty.Text
objDR("Product") = ddProducts.SelectedItem.Text
objDR("Cost") = Decimal.Parse(ddProducts.SelectedItem.Value)
objDT.Rows.Add(objDR)
Session("Cart") = objDT

dgCart.DataSource = objDT
dgCart.DataBind()

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top