Te paso un ejemplo que te puede servir, tiene los metodos para agregar y quitar productos, fijate que también si ya existe el producto y agregas un producto existente suma la cantidad. También veras que toma el id del producto que viene en este caso de la grilla, vos lo podes adaptar o usar o comparar con el tuyo a tu gusto.
Código:
Protected Sub AgregarCarrito(ByVal index)
Dim id As Integer = grid1.DataKeys.Item(index).Value
If Session("carrito") Is Nothing Then
CrearCarrito()
End If
Dim tabla As DataTable = Session("carrito")
If tabla.Rows.Contains(id) Then
tabla.Rows.Find(id).Item("prod_cantidad") += 1
Else
Dim fila As DataRow = tabla.NewRow
fila("id_producto") = id
fila("prod_nombre") = CType(grid1.Rows(index).FindControl("lblNombre"), Label).Text
fila("prod_cantidad") = 1
tabla.Rows.Add(fila)
End If
End Sub
Protected Sub CrearCarrito()
Dim Tabla As New DataTable
Tabla.Columns.Add("id_producto", GetType(Integer))
Tabla.Columns.Add("prod_nombre", GetType(String))
Tabla.Columns.Add("prod_cantidad", GetType(Integer))
Tabla.Columns.Add("prod_alquilar", GetType(Boolean)).DefaultValue = 0
Dim Clave() As DataColumn = {Tabla.Columns("id_producto")}
Tabla.PrimaryKey = Clave
Session("carrito") = Tabla
End Sub
Protected Sub ComandoControlar(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grid1.RowCommand
If e.CommandName = "CarritoAgregar" Then
AgregarCarrito(e.CommandArgument)
Else
PeliculaAlquilar(e.CommandArgument)
End If
End Sub
Protected Sub PeliculaAlquilar(ByVal index)
If Session("carrito") Is Nothing Then
CrearCarrito()
End If
Dim tabla As DataTable = Session("carrito")
Dim id As Integer = grid1.DataKeys.Item(index).Value
If tabla.Rows.Contains(id) Then
tabla.Rows.Find(id).Item("prod_cantidad") += 1
Else
Dim fila As DataRow = tabla.NewRow
fila("id_producto") = id
fila("prod_nombre") = CType(grid1.Rows(index).FindControl("lblNombre"), Label).Text
fila("prod_cantidad") = 1
fila("prod_alquilar") = 1
tabla.Rows.Add(fila)
End If
End Sub
Protected Sub CarritoQuitar(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
Try
Dim tabla As DataTable = Session("carrito")
Dim id As Integer = GridCarrito.DataKeys.Item(e.RowIndex).Value
If tabla.Rows.Find(id).Item("prod_cantidad") > 1 Then
tabla.Rows.Find(id).Item("prod_cantidad") -= 1
Else
tabla.Rows.Find(id).Delete()
End If
GridCarrito.DataSource = tabla
GridCarrito.DataBind()
Catch ex As Exception
Response.Write("A ocurrido un error: " & ex.Message)
End Try
End Sub
Recorrer los registros de una datatable:
For Each xRegistro As DataRow In tabla.Rows
Next