Thursday, March 22, 2012

Session variables

I'm designing like a shopping cart and have a product list which is an xml file consisting of products and prices which is loaded into a dataset then bound to a datagrid

There are buttons next to each product by clicking one of these should direct the user to another form showing the choice of product and price in textboxes, I can't load the info into the next form using session variables

My code is below

1st form code

Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the path of XML file and schema
Dim strPath As String = Server.MapPath(".")
' Declare a dataset
Dim dsProducts As New DataSet
' Apply the XML schema to the dataset
dsProducts.ReadXmlSchema(strPath & "\Products.xsd")
' Read the XML into the data set
dsProducts.ReadXml(strPath & "\Products.xml")
DataGrid1.DataSource = dsProducts
DataGrid1.DataBind()
Session.Item("Product1") = "Jam Doughnut"

End Sub

Private Sub btnJam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJam.Click

Server.Transfer("order.aspx")

2nd form code

Dim product1 as string = session.item("Product1")

End Sub

As you can see I'm trying to create the session variables before the next form is loaded but can't seem to load these into the next form, for example if jam doughnut is selected then jam doughnut and 0.35 should appear in textboxes on the next form or if apple doughnut is selected then apple doughnut and 0.40 should appear in the texboxes on the next form instead

Any advice would be appreciated

ThanksI don't understand your problem. On your next page when you do...

<% Session.Item("Product") %>

does it list the item on the page?
So what is preventing you from using it?
If the dataset isn't too large, you might want to consider just loading the dataset into a session and then pulling the data out on each page.

Session("products") = dsProducts

then on each page you can get it back:

dim ds as DataSet = Ctype(Session("products"),DataSet)

then you have your stuff in ds...
Thanks, I'm stuck now on how to retrieve the info from the dataset

I've put session("products")= dsproducts after datagrid.databind in the form load event procedure and Dim ds As DataSet = CType(Session("products"), DataSet) in the next form

I will need to put server.transfer("order.apx")for the 5 button click event procedures depending on what button is clicked then load the required choice in the textboxes on the next form

Any advice would be appreciated

Thanks
There are buttons next to each product by clicking one of these should direct the user to another form showing the choice of product and price in textboxes

Don't use session variables then. Redirect the user to

Product.aspx?productid=2389472

Where 2389472 is the ID of the product. In Product.aspx, query the database based upon the querystring parameter and get the info you need.

0 comments:

Post a Comment