How do i update in String Array

WebSpider 76 Reputation points
2021-07-22T06:34:05.407+00:00

Hi guys.. I wanna build a shopping cart using Cookies. I wonder how can I update quantity when added the same item. TQ

The following is the data inside CartList (PID, Quantity)
?CartList
"5001,1|5002,1|5003,1"

    Public Sub EditCart(ByVal PID As String, ByVal quantity As Integer)

        If Request.Cookies("CartCookie") Is Nothing = False Then

            Dim CartList As String = Request.Cookies("CartCookie").Value.ToString()
            Dim CartListSplitter As String() = CartList.Split("|"c)

            For Each items As String In CartListSplitter

                If Not items = "" Then
                    Dim item As String() = items.Split(","c)
                    If PID = item(0) Then
                        item(1) += quantity
                        // do something to update quantity and save to CartCookies
                        Exit For
                    End If
                End If

            Next

            Response.Redirect(Request.RawUrl, False)

        End If

    End Sub
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,096 Reputation points
    2021-07-22T08:35:23.66+00:00

    Hi @WebSpider ,
    I'm guessing that your operations of shopping are like this: firstly, when users add the quantities, it need to pass the PID and the quantity to code behind. So it need you to use ajax webmethod. And then you could update the quantity.Also you want to save the data to cookies,you could do like this:

    Response.Cookies["CartCookie"].Value =quantity.ToString();  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. WebSpider 76 Reputation points
    2021-07-23T01:23:55.567+00:00

    Hi @Yijing Sun-MSFT

    You're right, i will pass PID and Quantity to the code behind. This is how I add items to the cart.

    • Existing cart cookies is 5001,1|5002,1|5003,1
    • Will change to 5001,1|5002,1|5003,1|5004,1 if added new item.
    • Just wonder how can i change to 5001,1|5002,1|5003,2 if item (5003) has been added twice.
        Dim cartListItems = New List(Of String)() From {
             PID,
             quantity
         }
         AddCart(cartListItems)
      
      
        Public Sub AddCart(ByVal listCookie As List(Of String))
      
            Dim CartList As String = String.Join(",", listCookie)
      
            If Request.Cookies("CartCookie") Is Nothing Then
                Dim CartCookie As New HttpCookie("CartCookie")
                Response.Cookies("CartCookie").Value = CartList
            Else
                Response.Cookies("CartCookie").Value = Request.Cookies("CartCookie").Value & "|" & CartList
            End If
      
            Response.Cookies("CartCookie").Expires = DateTime.Now.AddYears(1)
            Response.Redirect(Request.RawUrl, False)
      
        End Sub
      

  2. WebSpider 76 Reputation points
    2021-07-23T05:45:58.883+00:00

    Yes. You're right.

    I wanna replace the old string and update the cookies accordingly.


  3. WebSpider 76 Reputation points
    2021-07-26T05:31:19.87+00:00

    Hi @Yijing Sun-MSFT

    Tried and quantity will be changed accordingly but will replace 5001 to 5002 as well.

    "5001,1|5002,1|5003,1"
    to
    "5002,2|5002,1|5003,1"

    Please advise. TQ


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.