Can some please help me convert an asp.net vb code to c#?

shopping cart systems
Ddp asked:


%@ Page debug=true Language=VB ContentType=text/html ResponseEncoding=iso-8859-1 %
html
head
titleShopping Cart/title
script runat=server
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow

Private Sub Page_Load(s As Object, e As EventArgs)
If Not IsPostBack Then
makeCart()
End If
End Sub

Function makeCart()
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(Product, GetType(String))
objDT.Columns.Add(Cost, GetType(Decimal))

Session(Cart) = objDT
End Function

Sub AddToCart(s As Object, e As EventArgs)
objDT = Session(Cart)
Dim Product = ddlProducts.SelectedItem.Text
Dim blnMatch As Boolean = False

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

If Not blnMatch Then
objDR = objDT.NewRow
objDR(Quantity) = txtQuantity.Text
objDR(Product) = ddlProducts.SelectedItem.Text
objDR(Cost) = Decimal.Parse(ddlProducts.SelectedItem.Value)
objDT.Rows.Add(objDR)
End If
Session(Cart) = objDT

dg.DataSource = objDT
dg.DataBind()

lblTotal.Text = $ GetItemTotal()
End Sub

Function GetItemTotal() As Decimal
Dim intCounter As Integer
Dim decRunningTotal As Decimal

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

Return decRunningTotal
End Function

Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objDT = Session(Cart)
objDT.Rows(e.Item.ItemIndex).Delete()
Session(Cart) = objDT

dg.DataSource = objDT
dg.DataBind()

lblTotal.Text = $ GetItemTotal()
End Sub
/script

/head
body
form runat=server
Product:br
asp:DropDownList id=ddlProducts runat=server
asp:ListItem Value=4.99Socks/asp:ListItem
asp:ListItem Value=34.99Pants/asp:ListItem
asp:ListItem Value=14.99Shirt/asp:ListItem
asp:ListItem Value=12.99Hat/asp:ListItem
/asp:DropDownListbr
Quantity:br
asp:textbox id=txtQuantity runat=server /brbr
asp:Button id=btnAdd runat=server Text=Add To Cart onClick=AddToCart /brbr
asp:DataGrid id=dg runat=server ondeletecommand=Delete_Item
columns
asp:buttoncolumn buttontype=LinkButton commandname=Delete text=Remove Item /
/columns
/asp:DataGrid
brbr
Total:
asp:Label id=lblTotal runat=server /
/form
/body
/html

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG

1 comment to Can some please help me convert an asp.net vb code to c#?

  • lazy_gamer_04

    Yes I agree, is the BEST place to go to convert VB and C#. But one thing to add is that it is not always 100% correct, mostly with minor things.

    In C# arrays/arrayLists use square brackets – Array[ ], but VB uses parenthesis – Array( ). This does not always convert properly (i.e. converting an array from VB to C# will leave the ( ) instead of changing to [ ]).

    I noticed the session in VB Session(cart), this would be Session[cart] in C#. So FYI, you may get a few errors after converting on the minor things like this.

You must be logged in to post a comment.