Translating code from a DetailsView to a GridView
I am building a shopping cart and the example I am using uses a DetailsView control based on a sqlDataSource. There is a button in the details view that uses the following code to add an item to the shopping cart
This is the original code:
Protected Sub btnAdd_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnAdd.Click
' get values from data source
Dim dv As DataView
dv = SqlDataSource1.Select( _
DataSourceSelectArguments.Empty)
Dim dr As DataRowView = dv(0)
Dim ID As String = dr("ProductID")
Dim name As String = dr("Name")
Dim Price As Decimal
' get or create shopping cart
Dim cart As ShoppingCart
If Session("cart") Is Nothing Then
cart = New ShoppingCart()
Session("cart") = cart
Else
cart = Session("cart")
End If
' add item to cart
cart.AddItem(ID, name, Price)
End Sub
The RED portion is what is giving me a problem. I want to use a GridView control with a button on each row that will add the item to the cart.
This is the code I am using but I am not sure how to translate the RED portion. I tried a couple of things and they didn't work so I thought I would go back to square one and ask the experts here to see if anyone had any thoughts. Thanks for your help!
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' get values from data source
Dim dv As DataView
dv = SqlDataSource1.Select( _
DataSourceSelectArguments.Empty)
Dim dr As DataRowView = dv(0)
Dim username As String = Membership.GetUser.UserName
Dim eventid As Integer = dr("eventID")
Dim eventCode As String = dr("eventTypeCode")
Dim eventDesc As String = dr("eventDesc")
Dim eventfee As Decimal = dr("eventFee")
Dim quantity As Integer = "1"
Dim regItemType As String = dr("eventSportType")
' get or create shopping cart
Dim cart As shoppingCart
If Session("cart") Is Nothing Then
cart = New shoppingCart()
Session("cart") = cart
Else
cart = Session("cart")
End If
' add item to cart
cart.AddItem(username, eventid, eventCode, eventDesc, eventfee, quantity, regItemType)
End Sub
|