Here is my code i have shooping cart.i have created a table and bind this with GridView.Now i want to update this table through the GridView Edit Button(Suppose The Qty Field).How i will i do it.

Imports System.Data
Imports System.Data.SqlClient
Partial Class MyCart
    Inherits System.Web.UI.Page
    Dim DT As System.Data.DataTable
    Dim DR As DataRow
    Dim Match As Boolean = False
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Session("Cart") Is Nothing Then
                DT = Session("Cart")
                GridView1.DataSource = DT
                GridView1.DataBind()
                lbltotal.Text = TotalAmount(DT)
                lbltotalitem.Text = DT.Rows.Count & " Items"
                lbltotalitemprice.Text = TotalAmount(DT)
                dv.Visible = True
            Else
                dv.Visible = False
            End If
        End If
    End Sub
    Function TotalAmount(ByVal DT As DataTable) As Decimal
        Dim returntotal As Decimal
        For Each DR In DT.Rows
            returntotal += DR("saleprice") * DR("Qty")
        Next
        Return returntotal
    End Function
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        DT = Session("Cart")
        DT.Rows(e.RowIndex).Delete()
        Session("Cart") = DT
        GridView1.DataSource = DT
        GridView1.DataBind()
        lbltotal.Text = TotalAmount(DT)
    End Sub
    
End Class

-------------------------------------------------------------------------

Imports System.Data
Imports System.Data.SqlClient
Partial Class ProductInformation
    Inherits System.Web.UI.Page
    Dim DT As DataTable
    Dim DR As DataRow
    Dim Match As Boolean = False
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim Con As New SqlConnection
            Con.ConnectionString = "Data source=SOFT-D5F98EFF93;Initial Catalog=ECommerce;Integrated Security=True"
            Dim Com As New SqlCommand("Select * from Products Where ProductID='" + Request.QueryString("ID") + "'", Con)
            Dim DR As SqlDataReader
            Con.Open()
            DR = Com.ExecuteReader
            If DR.HasRows Then
                While DR.Read
                    img1.ImageUrl = DR("Product Path")
                    lblid.Text = DR("ProductID")
                    lblisactive.Text = DR("IsActive")
                    lblsaleprice.Text = DR("SalePrice")
                End While
            End If
            Con.Close()
            If Session("Cart") Is Nothing Then
                MakeCart()
            End If

        End If
    End Sub
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        AddToCart()
        Dim str As String
        str = "<script language='javascript'>window.close();</script>"
        Response.Redirect("Close.aspx")
    End Sub
    Sub MakeCart()
        DT = New DataTable("MyCart")
        DT.Columns.Add("ID", GetType(Integer))
        DT.Columns.Add("SalePrice", GetType(Decimal))
        DT.Columns.Add("Qty", GetType(Integer))
        DT.Columns.Add("Total", GetType(Decimal))
        Session("Cart") = DT
    End Sub
    Sub AddToCart()
        DT = Session("Cart")
        MatchProduct(DT)
        If Not Match Then
            DR = DT.NewRow
            DR("ID") = lblid.Text
            DR("SalePrice") = Decimal.Parse(lblsaleprice.Text)
            DR("Qty") = txtqty1.Text
            DR("Total") = (Decimal.Parse(lblsaleprice.Text) * CType(txtqty1.Text, Integer))
            DT.Rows.Add(DR)
            Session("Cart") = DT
        End If

    End Sub
    Function TotalAmount(ByVal DT As DataTable) As Decimal
        Dim RV As Decimal
        For Each DR In DT.Rows
            RV += DR("Qty") * DR("Saleprice")
        Next
        Return RV
    End Function
    Sub MatchProduct(ByVal DT As DataTable)
        For Each DR In DT.Rows
            If DR("ID") = lblid.Text Then
                DR("Qty") = txtqty1.Text
                Match = True
                Exit For
            End If
        Next
    End Sub
End Class

protected sub WTVRNAMEHERE (ByVal sender as object, ByVal e as System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GRIDNAME.RowUpdated

here you grab the row ( e.affectedrows) and apply the changes in the datatable ( which is the initial datasource... all that of course after adding the "Edit/Update" button to your gridview

End Sub

This article has been dead for over six months. Start a new discussion instead.