Need help with ASP.net shopping cart, will pay $30 if you can fix this.

Discussion in 'C#' started by deltron, Aug 31, 2008.

  1. #1
    I am having trouble getting the quantity to update in my the cart, other then that it is fine.

    I am designing a site, its not complete but you can see it here: http://stibnite.evankendal.com. Browse through Office Furniture > Chairs > Task Seating. as this is the only category currently loaded. You can recreate the problem here, when you hit update nothing happens... Here is my code:

    I create the cart on the product page here:
    objCartDT = Session("Cart")
    If objCartDT Is Nothing Then
    objCartDT = New DataTable("Cart")
    objCartDT.Columns.Add("CartID", GetType(Integer))
    objCartDT.Columns("CartID").AutoIncrement = True
    objCartDT.Columns("CartID").AutoIncrementSeed = 1
    objCartDT.Columns.Add("ID", GetType(Integer))
    objCartDT.Columns.Add("Quantity", GetType(Integer))
    objCartDT.Columns.Add("item", GetType(String))
    objCartDT.Columns.Add("price", GetType(Decimal))
    objCartDT.Columns.Add("Comments", GetType(String))
    End If

    Then in the shopping cart page it is displayed in a data grid:
    <asp:datagrid ID="dgcart" runat="server" AutoGenerateColumns="false" GridLines="None" CellPadding="3" DataKeyField="CartID"
    OnEditCommand="dgcart_Edit" OnCancelCommand="dgcart_Cancel" OnUpdateCommand="dgcart_Update" OnDeletecommand="dgcart_Delete" BorderWidth="0px">
    <columns>
    <asp:ButtonColumn ButtonType="LinkButton" Text="Remove" CommandName="Delete" />
    <asp:EditCommandColumn EditText="Edit Quantity" CancelText="Cancel" UpdateText="Update" />
    <asp:BoundColumn DataField="Item" Headertext="Product Name" ReadOnly="true" />
    <asp:TemplateColumn HeaderText="Quantity">
    <ItemTemplate>
    <%#Container.DataItem("quantity")%>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="txtQuantity" runat="server" Text='<%# Container.DataItem("quantity")%>' Width="50px"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="Price" HeaderText="Cost" ReadOnly="true" />
    </columns>
    </asp:datagrid>

    Here is the behind code for the update button:
    Sub dgcart_edit(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    dgcart.EditItemIndex = e.Item.ItemIndex
    dgcart.DataSource = objcartDT
    dgcart.DataBind()
    End Sub
    Sub dgcart_Cancel(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    dgcart.EditItemIndex = -1
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    End Sub
    Sub dgcart_Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    objCartDT = Session("cart")
    Dim txtQuantity As TextBox
    Dim intItemID As Integer
    intItemID = dgcart.DataKeys(e.Item.ItemIndex)
    txtQuantity = e.Item.FindControl("txtQuantity")
    For Each objDR In objCartDT.Rows
    If objDR("ID") = intItemID Then
    objDR("Quantity") = Int32.Parse(txtQuantity.Text)
    Exit For
    End If
    Next
    dgcart.EditItemIndex = -1
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    Session("cart") = objCartDT
    End Sub

    PLEASE HELP ME! I am going crazy!! :eek:

    I can only pay with paypal.
     
    deltron, Aug 31, 2008 IP
  2. vihutuo

    vihutuo Well-Known Member

    Messages:
    1,511
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    180
    #2
    I think you need to call
    objCartDT.AcceptChanges()
    in the update event
     
    vihutuo, Aug 31, 2008 IP
  3. deltron

    deltron Active Member

    Messages:
    397
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Thanks for the help.. but I am still getting the same problem.

    I did some testing to narrow down the problem and it looks like I am having trouble retrieving the text that is typed into the textbox. I tried changing the starting value in the text box to a set number. Say 33 or something, and when i get update the quantity went from 1 to 33.. but i can't get it to retrieve the number you type into the box.

    so confused...[:S]
     
    deltron, Aug 31, 2008 IP
  4. vihutuo

    vihutuo Well-Known Member

    Messages:
    1,511
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    180
    #4
    Here is the code which works for me.


    ASPX file

    <asp:datagrid ID="dgcart" runat="server" AutoGenerateColumns="false" GridLines="None" CellPadding="3" DataKeyField="CartID"
    OnEditCommand="dgcart_Edit" OnCancelCommand="dgcart_Cancel" OnUpdateCommand="dgcart_Update" BorderWidth="0px">
    <columns>
    <asp:ButtonColumn ButtonType="LinkButton" Text="Remove" CommandName="Delete" />
    <asp:EditCommandColumn EditText="Edit Quantity" CancelText="Cancel" UpdateText="Update" />
    <asp:BoundColumn DataField="Item" Headertext="Product Name" ReadOnly="true" />
    <asp:TemplateColumn HeaderText="Quantity">
    <ItemTemplate>
    <%#Container.DataItem("quantity")%>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="txtQuantity" runat="server" Text='<%# Container.DataItem("quantity")%>' Width="50px"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="Price" HeaderText="Cost" ReadOnly="true" />
    </columns>
    </asp:datagrid>



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack() Then

    If objCartDT Is Nothing Then
    objCartDT = New Data.DataTable("Cart")
    objCartDT.Columns.Add("CartID", GetType(Integer))
    objCartDT.Columns("CartID").AutoIncrement = True
    objCartDT.Columns("CartID").AutoIncrementSeed = 1
    objCartDT.Columns.Add("ID", GetType(Integer))
    objCartDT.Columns.Add("Quantity", GetType(Integer))
    objCartDT.Columns.Add("item", GetType(String))
    objCartDT.Columns.Add("price", GetType(Decimal))
    objCartDT.Columns.Add("Comments", GetType(String))
    Dim row As Data.DataRow
    row = objCartDT.NewRow()
    row("ID") = 1
    row("Quantity") = 10
    row("item") = "ppp"
    row("price") = 5
    row("Comments") = "good"


    objCartDT.Rows.Add(row)
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    Session("Cart") = objCartDT

    End If
    End If
    End Sub


    Sub dgcart_edit(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    dgcart.EditItemIndex = e.Item.ItemIndex
    objCartDT = Session("Cart")
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    End Sub


    Sub dgcart_Cancel(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    dgcart.EditItemIndex = -1
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    End Sub


    Sub dgcart_Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    Dim objDR
    objCartDT = Session("cart")
    Dim txtQuantity As TextBox
    Dim intItemID As Integer
    intItemID = dgcart.DataKeys(e.Item.ItemIndex)
    txtQuantity = e.Item.FindControl("txtQuantity")
    For Each objDR In objCartDT.Rows
    If objDR("ID") = intItemID Then
    objDR("Quantity") = Int32.Parse(txtQuantity.Text)
    Exit For
    End If
    Next
    objCartDT.AcceptChanges()
    dgcart.EditItemIndex = -1
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    Session("cart") = objCartDT
    End Sub
     
    vihutuo, Sep 1, 2008 IP
  5. deltron

    deltron Active Member

    Messages:
    397
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #5
    it seems to work, but then I tried using it and it doesn't in the program. When you add a second row it fails..

    as a test I just added:
    Dim row2 As Data.DataRow
    row2 = objCartDT.NewRow()
    row2("ID") = 12
    row2("Quantity") = 10
    row2("item") = "asfgasdg"
    row2("price") = 5
    row2("Comments") = "good"
    objCartDT.Rows.Add(row2)

    Only the first row was editable
     
    deltron, Sep 3, 2008 IP
  6. nivedita2104

    nivedita2104 Peon

    Messages:
    118
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sub dgcart_edit(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    dgcart.EditItemIndex = e.Item.ItemIndex
    objCartDT = Session("Cart")
    dgcart.DataSource = objCartDT
    dgcart.DataBind()
    End Sub


    replace above code with below code then try

    Sub dgcart_edit(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    DataGrid1.DataBind();
    End Sub
     
    nivedita2104, Sep 3, 2008 IP
  7. vihutuo

    vihutuo Well-Known Member

    Messages:
    1,511
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    180
    #7
    I added a button named "btnAdd" and then in the click event handler typed the following code

    <asp:Button ID="btnAdd" runat="server" CausesValidation="False" Text="Add"/>

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    objCartDT = Session("cart")

    Dim row As Data.DataRow
    row = objCartDT.NewRow()
    row("ID") = 2
    row("Quantity") = 120
    row("item") = "jepppp"
    row("price") = 4
    row("Comments") = "bad"
    objCartDT.Rows.Add(row)

    dgcart.DataSource = objCartDT
    dgcart.DataBind()

    End Sub



    The added row can be modified
     
    vihutuo, Sep 3, 2008 IP