C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Shopping Cart,Datatable in Session


Posted Date: 27 Jan 2008    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: syedshanuMember Level: Bronze    
Rating: 1 out of 5Points: 10



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




Responses

Author: Nirav M. Mehta    01 Feb 2008Member Level: Gold   Points : 0
Ya it's very useful.

Thax buddy. For giving such a good script.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Shopping Cart  .  Datatable in Session  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Format Date
Previous Resource: Adding attributes such as backgorund color, forecolor to the particular item of a dropdownlist
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use