Shopping Cart Using Datatabel at Session This Artical deals with the sample Application for creating the Shopping Cart.In this artical user can learn how to add items,Remove items from the Shoping Cart,Adding and Removing items are done by Storing DataTable in Session.i think this will be usefull to many users
In all Pages Please Import this
Imports System.Data
Imports System.Text
Imports System.IO
Imports System.Data.SqlClient
Page 1
I have a Button in button Click i created Columns for Data Table and Assing the DataTable to Session.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
dt.Columns.Add("ItemId")
dt.Columns.Add("ItemName")
dt.Columns.Add("ItemPrice")
'ds.Tables.Add(dt)
Session("dsSessionVar") = dt
Server.Transfer("Copy of Default.aspx")
End Sub
Page 2
i have added three textboxes and a button in this form to add items to cart
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If IsNothing(Session("dsSessionVar")) Then
Response.Redirect("default.aspx")
End If
Catch ex As Exception
End Try
End Sub
'Button Click
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As DataSet
Dim dt As New DataTable
dt = DirectCast(Session("dsSessionVar"), DataTable)
'dt = Session("dsSessionVar")
Dim dr As DataRow
dr = dt.NewRow()
dr("ItemId") = TextBox1.Text
dr("ItemName") = TextBox2.Text
dr("ItemPrice") = TextBox3.Text
dt.Rows.Add(dr)
Response.Redirect("Copy of Copy of Default.aspx")
End Sub
Page 3
3) In third form user can view the added Cart Item and can add more items and also can remove the Items from the Cart.
'Add MoreItems Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Server.Transfer("Copy of Default.aspx")
End Sub
'Page Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If IsNothing(Session("dsSessionVar")) Then
Response.Redirect("default.aspx")
End If
If Not Page.IsPostBack Then
Dim dt As New DataTable
dt = DirectCast(Session("dsSessionVar"), DataTable)
GridView1.DataSource = dt
GridView1.DataBind()
End If
Catch ex As Exception
End Try
End Sub
'To go to Home Page
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Server.Transfer("default.aspx")
End Sub
''To Remove Items from the Cart
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim dt As New DataTable
dt = DirectCast(Session("dsSessionVar"), DataTable)
Dim ival As Integer
For ival = 0 To dt.Rows.Count - 1
If dt.Rows(ival).Item("ItemId") = TextBox1.Text.Trim Then
dt.Rows(ival).Delete()
Exit For
End If
Next
dt.AcceptChanges()
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
|
| Author: Nirav M. Mehta 01 Feb 2008 | Member Level: Gold Points : 0 |
Ya it's very useful.
Thax buddy. For giving such a good script.
|