question

WebSpider-5357 avatar image
0 Votes"
WebSpider-5357 asked YijingSun-MSFT commented

How do i update in String Array

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





dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
1 Vote"
YijingSun-MSFT answered

Hi @WebSpider-5357 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WebSpider-5357 avatar image
0 Votes"
WebSpider-5357 answered YijingSun-MSFT commented

Hi @YijingSun-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



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @WebSpider-5357 ,

Just wonder how can i change to 5001,1|5002,1|5003,2 if item (5003) has been added twice.

So,can I understand your problem is that you don't know how to change "5003,2" of the string list?You have split the string and change the quantity but you don't know how to replace the old string. I don't make sure my guessing is right.Could you tell us?
Best regards,
Yijing Sun
0 Votes 0 ·
WebSpider-5357 avatar image
0 Votes"
WebSpider-5357 answered YijingSun-MSFT commented

Yes. You're right.

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @WebSpider-5357 ,
I think you could use replace.I have create a demo.Just like this:

             var PID = "5003";
             int quantity = 1;
             string CartList = "5001,1|5002,1|5003,1";
             string[] CartListSplitter = CartList.Split('|');
             foreach (string items in CartListSplitter)
             {
                 if (items != "")
                 {
                     string[] item = items.Split(',');
                     int x = Convert.ToInt32(item[1]);
                     if (PID == item[0])
                     {
                         x += quantity; 
                     }
                   var itemreplace=  items.Replace(char.Parse(item[1]), char.Parse(x.ToString()));
                   var CartListreplace=CartList.Replace(items, itemreplace);
                   Response.Cookies["CartCookie"].Value = CartListreplace;
                 }
             }

Best regards,
Yijing Sun

0 Votes 0 ·
WebSpider-5357 avatar image
0 Votes"
WebSpider-5357 answered YijingSun-MSFT commented

Hi @YijingSun-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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @WebSpider-5357 ,
I suggest you could debug and break point at these lines to check what's the values step by step. In the example codes, CartList is your string. The CartListSplitter have three item.And then you for loop the list. The item[1] is the quantity. When you calculate the quantity, you could use replace the old value. You could check step by step.
Best regards,
Yijing Sun

0 Votes 0 ·