basket adding a new row rather than updating the same row

 Hi,

 I hope someone can help me?  I have an add to basket application which does not quite work, in that when try to add the same item to the basket, rather than updating the same row it adds another row instead.  I am using a session datagrid type basket from code I incorporated after looking the the sitepoint website  http://www.sitepoint.com/article/net-shopping-cart-datatables, though I know its dated it seems to be ok.

I think the problem may lie with the following bit of code, in that it is not updating the table correctly somehow but not sure from this point, any help is appreciated

 

   Dim blnMatch As Boolean = False

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

        Next

        If Not blnMatch Then
            objDR = objDT.NewRow
            objDR("Quantity") = Int32.Parse(txtQuantity.Text)
            objDR("Product") = ddlproducts.SelectedItem.Text
            objDR("Name") = myProduct.name
            objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
            objDT.Rows.Add(objDR)


        End If

        displayCart()
    End Sub
 

 

Regards,

 

Anteater 

0
anteater
3/7/2008 9:59:02 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

4 Replies
455 Views

Similar Articles

[PageSpeed] 45

According to the code you posted, the only way what you're describing can happen is that objDR("Products") = Product never evaluates to true. That's where you have to focus.

It's difficult to diagnose further without knowing the schema for objDT. Is "Products" an actual column name in the table? Or is it supposed to be 'Product" instead? And is Products a data type, a text value, or what? And if Product is a data type, should you be comparing the whole Product, or a specific property of Product?


Visit my blog
0
Lee
3/7/2008 11:35:03 PM

 HI,

 Here is the rest of the code,

 
I have a products page where parts are filled in with label controls from the database. 

this is the code for the .vb page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim productcode As Integer = Request.QueryString("id")

        Dim myProduct As product = productaccesslayer.getproducts(productcode)


        lblname.Text = myProduct.name
        lblref.Text = myProduct.ref
        lblsize.Text = myProduct.size
        lbldescription.Text = myProduct.description
        pictureUrl.ImageUrl = Request.ApplicationPath + myProduct.pictureUrl
        'lblproductname.Text = myProduct.name

        If Not IsPostBack Then
            displayCart()
        End If

        'If Not IsPostBack Then
        '    Me.lblproductname.Text = myProduct.name
        'End If

    End Sub

    Public Sub displayCart()

        If Session("Cart") Is Nothing Then
            Dim objDT As System.Data.DataTable
            'Dim objDR As System.Data.DataRow

            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("Products", GetType(String))
            objDT.Columns.Add("Name", GetType(String))
            objDT.Columns.Add("Cost", GetType(Decimal))

            Session("Cart") = objDT
        End If

        GridView1.DataSource = CType(Session("Cart"), System.Data.DataTable)
        GridView1.DataBind()

        lblTotal.Text = "£" & GetItemTotal()
    End Sub



    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim objDT As System.Data.DataTable
        Dim objDR As System.Data.DataRow

        objDT = Session("Cart")

        Dim Product = ddlproducts.SelectedItem.Text

        Dim productcode As Integer = Request.QueryString("id")
        Dim myProduct As product = productaccesslayer.getproducts(productcode)



        objDR = objDT.NewRow
        objDR("Quantity") = txtQuantity.Text
        objDR("Products") = ddlproducts.SelectedItem.Text
        objDR("Name") = myProduct.name
        objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
        objDT.Rows.Add(objDR)
        Session("Cart") = objDT

       
        Dim blnMatch As Boolean = False

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

        Next

        If Not blnMatch Then
            objDR = objDT.NewRow
            objDR("Quantity") = Int32.Parse(txtQuantity.Text)
            objDR("Product") = ddlproducts.SelectedItem.Text
            objDR("Name") = myProduct.name
            objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
            objDT.Rows.Add(objDR)


        End If

        displayCart()
    End Sub


    Function GetItemTotal() As Decimal

        Dim intCounter As Integer
        Dim decRunningTotal As Decimal = 0

        Dim objDT As System.Data.DataTable
        Dim objDR As System.Data.DataRow

        objDT = Session("Cart")

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

        Return decRunningTotal

    End Function



End Class

 

 

 

this is the code behind for the .aspx page that relates to the basket

 

<asp:Label ID="lblproductname" runat="server" Visible="False"></asp:Label>--%>
    <asp:DropDownList ID="ddlproducts" runat="server" Style="left: -8px; position: relative;
        top: 64px" Visible="False">
        <asp:ListItem Value="20.00"></asp:ListItem>
    </asp:DropDownList>
    <asp:TextBox ID="txtQuantity" runat="server" Style="left: -196px; position: relative;
        top: 102px" Width="30px"></asp:TextBox>
    <asp:GridView ID="GridView1" runat="server" Style="left: 196px; position: relative;
        top: 19px" Height="58px" Width="171px">
    </asp:GridView>Basket Total:
    <asp:Label ID="lblTotal" runat="server" Style="left: 7px; position: relative; top: -2px"
        Text=" "></asp:Label>
    <asp:Button ID="btnAdd" runat="server" Style="left: -253px; position: relative; top: 0px"
        Text="add to basket" />

 

I wrote the price into the dropdown list and hid it so all that is visible on the page is the textbox, label, gridview and button

I hope this helps

 

Anthony 

 


 

0
anteater
3/8/2008 9:37:16 AM

 You're adding two rows in the btnAdd_Click method. You need to set a breakpoint at the top of this method, then run the debugger to see it happening.

http://msdn2.microsoft.com/en-us/library/z9e7w6cs(VS.80).aspx 


Visit my blog
0
Lee
3/8/2008 3:40:06 PM

 Hi,

 Thank you very much for this.  I will reply shortly, I am going through the link you sent to get a better understanding of this.

Regards,

Anteater
 


 

0
anteater
3/9/2008 10:16:24 PM
Reply:

Similar Artilces:

Adding a new row between rows
Hi to all. I'm trying to do a sort of SubReport. At this moment I have This Code made. It verifies if the column is equal it will erase it's text. What I would like to do now is if the Cell is not Equal then it will create a row with a label that says Sub Report. I tried some code but it inserts the row at the end. Can any one help me? protected void gridDefectReport_RowDataBound(object sender, GridViewRowEventArgs e)    {         if (e.Row.RowIndex > 0)        {     &nb...

Row updating, row updated confusion
Okay this is the first time I've ever had to do code behind for row updating logic in a gridview. So be patient with me if it's totally incorrect. Do I put the gridview rebind code in the Row Updating or Updated event?  Currently, I have it in both as I'm simply troubleshooting to try to get it to work.  With this code, not only is the record not updating but the record disappears from the gridview upon refreshing.Protected Sub GridView2_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView2.RowUpdated ...

adding a blank header row /row above the header row
is it possible to add a blank row above the header row in gridview for adding some links and buttons the links will respond to thae data in the grid and the buttons are for login / regisration etc....  if yes how it can be done or how do i achieve it. Its better that u create a table with a single row with columns matching the GridView columns. Set the table width same as the GridView width. This will make the GridView Look as if there is a row above its header.Please do not forget to click Mark As Answer on the post that helped you. Regards,Iam Srikanth Reddy....

how do I get the row index of the row that was just updated in the row_updated function?
How do I get the row index of the row that was just updated in the row_updated function?  Please don't tell me to use the row updating function because I tried to do what I want under row_updating but it doesn' t work for what I need to do.   Thanks. aspusername: Please don't tell me to use the row updating function Sorry, but that's where the information is: int LastRowUpdated; protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { LastRowUpdated = e.RowIndex; } protected void GridView1_RowUpdated...

question about updating a row? How to update a different row.
I will try to make this as simple as I can. k, here I go. I originally populate my datagrid with data. Then during the edit mode, I can populate information by an ontextchanged event, which is actually bringing in a new row (essentially). My problem occurs when I try to update the data. Since the row was originally populated with the data, it updates that row in the table. But since I did an ontextchanged, I want to update that row instead. Not the original row. If I do an update, then I have two identical rows of data in my table, but with different primary keys. How can I update t...

Trying to delete a row, I get "Row changed between retrieve and update."
Hi Trying to delete a row I get the following error message and can't figure out why. Please see error below and any suggestion will be appreciated. Thanks Error message and part of the script: Row changed between retrieve and update. No changes made to database. DELETE FROM q_code_cause WHERE code = 1 AND ... Ok I found the Problem! Thanks anyway "Wasap" <edgadon@medicalcardsystem.com> wrote in message news:3f96a7a5@forums-2-dub... > Hi > > Trying to delete a row I get the following error message and can't figure > out why. Ple...

update multi row when i meant update single row
RGVhciBhbGw6DQoNCkkgdHJpZWQgdG8gdXBkYXRlIGEgc2luZ2xlIHJvdyBmb3IgdGhlIGRh dGFiYXNlLCBob3dldmVyLCB0aGUgcXVlcnkgaGVyZQ0KdXBkYXRlIG11bHRpcGxlIHJvd3MN Cg0KbXkgJHNxbCA9ICJVUERBVEUgdXB0aW1lIFNFVCB1cF90aW1lPSRuZXdfdXB0aW1lDQog ICAgICAgICAgICAgV0hFUkUgaG9zdG5hbWUgPSAnJGhvc3QnIGFuZA0KICAgICAgICAgICAg IHN0YXJ0ZGF0ZT0gKHNlbGVjdCBNQVgoc3RhcnRkYXRlKSBmcm9tIHVwdGltZSB3aGVyZQ0K ICAgICAgICAgICAgIGhvc3RuYW1lID0nJGhvc3QnIGFuZCB1cF90aW1lID0gJG9sZHVwdGlt ZSkgIjsNCg0KSSBtZWFudCBvbmx5IHdhbnQgdG8gdXBkYXRlIGxhdGVzdCBzdGFydGRhdGUs IHNvbWVob3csIGl0IHJldHVybnMgYWxsDQp0aGUgc3RhcnRkYXRlIHdoaWNoIHVwX3RpbWUg PSRvbGR1...

warning: update row ... is a conflict update and this row is ignored
SQL Anywhere 10.0.1.3488 I am synching one SQL Anywhere database to another through MobiLink, getting this warning: W. 2008-08-19 13:01:01. <17> [10037] Ignoring updated row (new values) W. 2008-08-19 13:01:01. <17> [10038] Ignoring updated row (old values) W. 2008-08-19 13:01:01. <17> [10072] The update row for table '<orders>' is a conflict update and this row is ignored. There are currently no conflict resolution scripts in place, so I would expect the default behavior of "last one in wins" rather than seeing "this row is ign...

Returning n rows, setting start row, total number of rows
Hi, I'm try to find out how ado.net can perform some of the things i am able to do in ado. Here is the scenario: I will be searching a table that may have 1000's of rows. I want to be able to dictate how many rows (page size) to return and what the starting row number is and be able to find out how many total rows would be returned by the query. Using old ado recordsets I was able to get the recordset and get the count or all the rows (.recordcount - ormaybe it was .rowcount) and then jump to the starting position (.absoluteposition). How can I do the equivalent in .net? From loo...

Updating DataList Row by Row
Hello, I have a datalist on my web page which displays a list of records from my database, all the information is displayed in labels except for one which is in a DropDownBox and the user can select Yes or No.  I want to update each record individual based on the value in the dropdownbox.  My problem is currently if I change the last record to say No then all the records in the db change to No.  It is not recording the new values row by row. Anybody got ideas why? ThanksRegards,Steven I don't think is the DataList because I used in that way myself many times Post som...

Adding Dynamic row to Row
I have written the following code in javascript. This code is working fine in FireFox but not in IE 7.Need help so that it should  also work fine in IE7.Thanks.  var repeater= document.getElementById('<%=repeater.ClientID %>');        var obj = result;        var newRow = document.createElement("tr");        var newCell = document.createElement("td");        var ctrlInputHid = document.createElement("input...

Getting dw update for DataMod! row previously New!
Hello, First-time poster here. I have run into a strange problem with the dw update function in regards to rows that have been changed from New! to DataModified! I have two datawindows that are from the same table that I am doing a RowsMove from one to the other. I simply want the destination datawindow to handle the row in the dw update function with an SQL Update statement. I do a SetItemStatus on the row to DataModified which should cause an SQL Update instead of an Insert. The dw update function is run and doesn't return an error. But when I look at the SQLSpy window...

How to set the row number and get the row number for ("selected row) in Datagrid
I face a problem that i can't get the selected row of the datagrid in both server side script (VB.net) and client side script (javascript) As i can't use the button column / hyperlink column in the datagrid. Thus , i need to handle the change the background color of the selected row and also to get the selected row number for Update/Delete. Thanks in advance. This depends on how do you perform the selection. Do you want to implement chaning of the SelectedRow at the client side without any postback? If you have no issues with postback, then you can do s little trick here: 1. ...

(Fwd) How to loop through a database, row by row, and select and update one row at a time
----- Forwarded message from Troy Mulder <mulderta@gmail.com> ----- Date: Mon, 12 Apr 2010 17:48:37 -0400 From: Troy Mulder <mulderta@gmail.com> To: Tim.Bunce@pobox.com Subject: How to loop through a database, row by row, and select and update one row at a time Hello Tim (is it Dr. Bunce?), My name is Troy Mulder, and I am trying to get a perl script to interface with a PostgreSQL database. I am trying to step through each row of the database, and read one column of the row, and update another column of the row. When I follow the online tutorial a...

Web resources about - basket adding a new row rather than updating the same row - asp.net.getting-started

Wikipedia:Updating information - Wikipedia, the free encyclopedia
This page documents an English Wikipedia editing guideline . It is a generally accepted standard that editors should attempt to follow, though ...

Facebook Updating Software Development Kit For Apple IOS 6 To Reflect Integration
With Facebook integration officially coming to Apple’s iOS 6 mobile operating system for its iPhone and iPad tablet, what new features are in ...

Updating how Cost per Click is Measured on Facebook
To help advertisers better understand how their ads perform, we're updating the definition of cost per click on Facebook. Learn what the change ...

Updating Your App for iOS 9: What You Need to Know
Updating Your App for iOS 9: What You Need to Know

Facebook for iPhone 3.0 to Include Page Updating
... who want to more easily update their Facebook Pages on the go: Facebook for iPhone 3.0 , which should be released soon, will include Page updating, ...

Updating trends on mobile
Trends are a great way to find out what people are talking about on Twitter at a given moment and at a single glance. Starting today, we’re making ...

Delhi Metro Fare Calculator and Auto-Updating Map on the App Store on iTunes
Get Delhi Metro Fare Calculator and Auto-Updating Map on the App Store. See screenshots and ratings, and read customer reviews.

BlackBerry Bold 9000 Updating #2 - Flickr - Photo Sharing!
Explore berrytokyo's photos on Flickr. berrytokyo has uploaded 126 photos to Flickr.

Freeview launches auto-updating EPG
Shows running late won't phase new Freeview PVRs, but the new Australian EPG doesn't work with all Freeview gear.

Tips For Updating Your Appearance
Like Joe Hockey, all men can pull off a radical mid-life makover by following some simple guidelines.

Resources last updated: 12/4/2015 11:38:14 PM