By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Weird issue with System.NullReferenceException

P: n/a
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

Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I think it would be helpful if you posted the code behind up.

Ciaran

"Shannon" <sh************@state.co.us> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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

Jul 21 '05 #2

P: n/a
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("C onnectRWData"))

'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

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.