ASP.NET 购物车

发布于 2024-10-16 04:03:51 字数 2947 浏览 3 评论 0原文

我正在使用 VB 后端在 ASP.NET 中构建一个非常简单的购物车,但我的代码遇到了问题。当我运行应用程序并尝试将产品添加到购物车时,我收到一条错误消息。

Object reference not set to an instance of an object. 

描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

源错误:

Line 27:         Dim blnMatch As Boolean = False
Line 28: 
Line 29:         For Each Me.objDR In objDT.Rows
Line 30:             If objDR("StockItemName") = Product Then
Line 31:                 objDR("Quantity") += txtQuantity.Text

我不确定为什么要这样做,希望有人可以看一下并提供一些建议?我检查了我的代码,没有发现任何错误,但是我会接受您可能拥有的任何指导。

这是我的代码。

Shoppingcart.aspx

<asp:DropDownList id="ddlProducts" runat="server">

袜子 裤子 衬衫 帽子
数量:






全部的:

Shoppingcart.aspx.vb 导入系统数据 部分公开课购物车 继承System.Web.UI.Page

Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        makeCart()
    End If
End Sub
'Mark Cart function
Function makeCart()
    objDT = CType(Session("Cart"), DataTable)
    objDT.Columns.Add("StockID", GetType(Integer))
    objDT.Columns("StockID").AutoIncrement = True
    objDT.Columns("StockID").AutoIncrementSeed = 1

    objDT.Columns.Add("StockItemName", GetType(String))
    objDT.Columns.Add("StockItemValue", GetType(Decimal))
    Session("Cart") = objDT
End Function
'This is for adding items to the shopping cart.
Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
    objDT = Session("Cart")
    Dim Product As String = ddlProducts.SelectedItem.Text
    Dim blnMatch As Boolean = False

    For Each Me.objDR In objDT.Rows
        If objDR("StockItemName") = Product Then
            objDR("Quantity") += txtQuantity.Text
            blnMatch = True
            Exit For
        End If
    Next

    If Not blnMatch Then
        objDR = objDT.NewRow
        objDR("Quantity") = txtQuantity.Text
        objDR("StockItemName") = ddlProducts.SelectedItem.Text
        objDR("StockItemValue") = Decimal.Parse(ddlProducts.SelectedItem.Value)
        objDT.Rows.Add(objDR)
        Session("Cart") = objDT
    End If

    dg.DataSource = objDT
    dg.DataBind()
End Sub
Function GetItemTotal() As Decimal
    Dim intCounter As Integer
    Dim decRunningTotal As Decimal

    For intCounter = 0 To objDT.Rows.Count - 1
        objDR = objDT.Rows(intCounter)
        decRunningTotal += (objDR("StockItemValue") * objDR("Quantity"))
    Next

    Return decRunningTotal
End Function
Sub Delete_Item(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

    objDT = Session("Cart")
    objDT.Rows(e.Item.ItemIndex).Delete()
    Session("Cart") = objDT

    dg.DataSource = objDT
    dg.DataBind()
    lblTotal.Text = "$" & GetItemTotal()
End Sub

结束类

I am in the middle of building a very simple shopping cart in asp.net with a VB backend but I am running into problems with my code. When I run my application and try to add a product to the cart I get an error message.

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:

Line 27:         Dim blnMatch As Boolean = False
Line 28: 
Line 29:         For Each Me.objDR In objDT.Rows
Line 30:             If objDR("StockItemName") = Product Then
Line 31:                 objDR("Quantity") += txtQuantity.Text

I am not sure why it is doing this and was hoping that perhaps someone could take a look and offer up some advice? I have checked over my code and I cannot find anything wrong however I would take any guidence that you may have on it.

Here is my code.

Shoppingcart.aspx

<asp:DropDownList id="ddlProducts" runat="server">

Socks
Pants
Shirt
Hat

Quantity:

Total:

Shoppingcart.aspx.vb
Imports System.Data
Partial Public Class Shoppingcart
Inherits System.Web.UI.Page

Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        makeCart()
    End If
End Sub
'Mark Cart function
Function makeCart()
    objDT = CType(Session("Cart"), DataTable)
    objDT.Columns.Add("StockID", GetType(Integer))
    objDT.Columns("StockID").AutoIncrement = True
    objDT.Columns("StockID").AutoIncrementSeed = 1

    objDT.Columns.Add("StockItemName", GetType(String))
    objDT.Columns.Add("StockItemValue", GetType(Decimal))
    Session("Cart") = objDT
End Function
'This is for adding items to the shopping cart.
Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
    objDT = Session("Cart")
    Dim Product As String = ddlProducts.SelectedItem.Text
    Dim blnMatch As Boolean = False

    For Each Me.objDR In objDT.Rows
        If objDR("StockItemName") = Product Then
            objDR("Quantity") += txtQuantity.Text
            blnMatch = True
            Exit For
        End If
    Next

    If Not blnMatch Then
        objDR = objDT.NewRow
        objDR("Quantity") = txtQuantity.Text
        objDR("StockItemName") = ddlProducts.SelectedItem.Text
        objDR("StockItemValue") = Decimal.Parse(ddlProducts.SelectedItem.Value)
        objDT.Rows.Add(objDR)
        Session("Cart") = objDT
    End If

    dg.DataSource = objDT
    dg.DataBind()
End Sub
Function GetItemTotal() As Decimal
    Dim intCounter As Integer
    Dim decRunningTotal As Decimal

    For intCounter = 0 To objDT.Rows.Count - 1
        objDR = objDT.Rows(intCounter)
        decRunningTotal += (objDR("StockItemValue") * objDR("Quantity"))
    Next

    Return decRunningTotal
End Function
Sub Delete_Item(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

    objDT = Session("Cart")
    objDT.Rows(e.Item.ItemIndex).Delete()
    Session("Cart") = objDT

    dg.DataSource = objDT
    dg.DataBind()
    lblTotal.Text = "$" & GetItemTotal()
End Sub

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-10-23 04:03:51

我复制了你的代码并运行了几次并弄清楚了。
在初始页面加载 Session("Cart") 没有任何内容。向数据表添加新列时导致对象引用错误。

将您的 DataTable 设置为等于 Session("Cart"),如下所示:

objDT = CType(Session("Cart"), DataTable)

将给它一个空值。相反,在初始页面加载时执行此操作:

objDT = New Data.DataTable

设置 Session("Cart") 后,可以像之前一样将数据表设置为等于 Session("Cart") 。

整个功能:

Function makeCart()
    objDT = New Data.DataTable
    objDT.Columns.Add("StockID", GetType(Integer))
    objDT.Columns("StockID").AutoIncrement = True
    objDT.Columns("StockID").AutoIncrementSeed = 1

    objDT.Columns.Add("StockItemName", GetType(String))
    objDT.Columns.Add("StockItemValue", GetType(Decimal))
    Session("Cart") = objDT
End Function

I copied your code and ran it through a couple of times and firgured it out.
On the inital page load Session("Cart") is nothing. Causing the Object Reference error when adding a new column to the DataTable.

Setting your DataTable equal to the Session("Cart") like this :

objDT = CType(Session("Cart"), DataTable)

will give it a value of nothing. Instead, do this on the inital page load :

objDT = New Data.DataTable

After Session("Cart") has been set you, can set the datatable equal to Session("Cart") like you did before.

The entire function :

Function makeCart()
    objDT = New Data.DataTable
    objDT.Columns.Add("StockID", GetType(Integer))
    objDT.Columns("StockID").AutoIncrement = True
    objDT.Columns("StockID").AutoIncrementSeed = 1

    objDT.Columns.Add("StockItemName", GetType(String))
    objDT.Columns.Add("StockItemValue", GetType(Decimal))
    Session("Cart") = objDT
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文