It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Need help with the syntax for a loop.

Moderator: dafunkymunky

Need help with the syntax for a loop.

Postby banksidepoet on Sun May 25, 2008 6:09 am

Hi, Can anyone help me with this syntax.

I have am ASP.NET form which lists products for sale. Each item has a textbox which invites the buyer to enter the number of that item she would like to buy (with a default of 0).

I have created a VB code behind file which processes the form and sends the data to PayPal.

The problem is that PayPal will (understandably) not accept "0" as an input for quantity. So, I need to strip the items that the user doesn't want any of before I send the items. The problem is further complicated because PayPal needs the items in the cart to be consecutively numbered so, I need to skip the "0" items but NOT the item count.

Here's the code. I've come up with a simple For... Next loop which skips creating POST code for "0" items but which does not increment the value of "i" if it skips (so maintaining the "consecutive" nature of the count).

1. Dim i As Integer
2. For i = 1 to 2

3. If TextBox1.Text >= 1 Then
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + "item_name_1" + """" + " value=" + """" + product1 + """" + ">")
4. i += 1
5. EndIf

6. If TextBox2.Text >= 1 Then
7. ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + "item_name_2" + """" + " value=" + """" + product2 + """" + ">")
8. i += 1
9. EndIf

10. Next i

Basically, the code retrieves the value of TextBox1 (how many of item 1 the buyer wants). If it is not 0, the code to send the details to PayPal is created and the item is the "first" item in the Cart. If it is 0, the code is not generated but "i" remains 1, so when it looks for the next quantity (TextBox2.Text) I can use the value of i to say to PayPal, "this" is the first item in the Cart.

But the code above will only work if I can replace "item_name_1" and "item_name_2" with "item_name_i".

What is the syntax to do this? I've tried all manner of concatenations and quotes but keep getting a malformed string error. I'm new to VB and not sure what the """"'s do. (The POST code I'm using was recommended to me by a much cleverer guy!)

I'd appreciate help if anyone's able.
banksidepoet
 
Posts: 2
Joined: Sun May 25, 2008 5:47 am

Postby banksidepoet on Sun May 25, 2008 8:50 am

Got a response from MSDN VB Forum:

Dim Count As Integer = 1
While Count < 2

If TextBox1.Text >= 1 Then
Dim item As String = "item_name_" & Count
Dim amount As String = "amount_" & Count
Dim quantity As String = "quantity_" & Count
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + item + """" + " value=" + """" + product1 + """" + ">")
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + amount + """" + " value=" + """" + price1 + """" + ">")
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + quantity + """" + " value=" + """" + number1 + """" + ">")
Count += 1
End If

If TextBox2.Text >= 1 Then
Dim item As String = "item_name_" & Count
Dim amount As String = "amount_" & Count
Dim quantity As String = "quantity_" & Count
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + item + """" + " value=" + """" + product2 + """" + ">")
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + amount + """" + " value=" + """" + price2 + """" + ">")
ppForm.Append("<input type=" + """" + "hidden" + """" + " name=" + """" + quantity + """" + " value=" + """" + number2 + """" + ">")
Count += 1
End If

End While

Thanks.
banksidepoet
 
Posts: 2
Joined: Sun May 25, 2008 5:47 am


Who is online

Users browsing this forum: No registered users and 6 guests