Pemberley1
asked on
Pass datatable between asp.net pages
I've made a shopping cart, but I need to be able to visit other pages within the same application without losing the values in my datatable adding new rows as people go through my site.
In other words, I need people to be able to add items from one page to their cart and then go to a different page and add more items. I'll also need to keep those values for the checkout page. How do I do this?
I've already got a session called "Cart". This datatable works fine as long as I only use the page that I've created it on, but I can't figure out how to pass the whole table to each page as they shop.
I've declared the datatable and data row at the public class WebForm level
DataTable objCartDT;
DataRow objDR;
Then this is in the page load event.
objCartDT = (DataTable)Session["Cart"] ;
if (objCartDT == null)
{
objCartDT = new DataTable("Cart");
objCartDT.Columns.Add("Car tID", typeof(Int32));
objCartDT.Columns["CartID" ].AutoIncr ement = true;
objCartDT.Columns["CartID" ].AutoIncr ementSeed = 1;
objCartDT.Columns.Add("Wag eType", typeof(string)); //lgart
objCartDT.Columns.Add("Tot alCost", typeof(Decimal));
objCartDT.Columns.Add("Ded uction", typeof(Decimal)); //Betrg
objCartDT.Columns.Add("Pho ne", typeof(string));
objCartDT.Columns.Add("Mem Name", typeof(string));
objCartDT.Columns.Add("Add r", typeof(string));
objCartDT.Columns.Add("Qua ntity", typeof(Int32)); //number of children
objCartDT.Columns.Add("Per nr", typeof(string));
objCartDT.Columns.Add("Use r", typeof(string)); //userid
objCartDT.Columns.Add("Beg Date", typeof(string)); //begin of deduction period
objCartDT.Columns.Add("End da", typeof(string)); // end of deduction period
objCartDT.Columns.Add("Nam e", typeof(string)); //name on membership
objCartDT.Columns.Add("Spo use", typeof(string)); //spouse on membership
objCartDT.Columns.Add("Ema il", typeof(string));
objCartDT.Columns.Add("Cit y", typeof(string));
objCartDT.Columns.Add("Sta te", typeof(string));
objCartDT.Columns.Add("Zip ", typeof(string));
Session["Cart"] = objCartDT;
}
In other words, I need people to be able to add items from one page to their cart and then go to a different page and add more items. I'll also need to keep those values for the checkout page. How do I do this?
I've already got a session called "Cart". This datatable works fine as long as I only use the page that I've created it on, but I can't figure out how to pass the whole table to each page as they shop.
I've declared the datatable and data row at the public class WebForm level
DataTable objCartDT;
DataRow objDR;
Then this is in the page load event.
objCartDT = (DataTable)Session["Cart"]
if (objCartDT == null)
{
objCartDT = new DataTable("Cart");
objCartDT.Columns.Add("Car
objCartDT.Columns["CartID"
objCartDT.Columns["CartID"
objCartDT.Columns.Add("Wag
objCartDT.Columns.Add("Tot
objCartDT.Columns.Add("Ded
objCartDT.Columns.Add("Pho
objCartDT.Columns.Add("Mem
objCartDT.Columns.Add("Add
objCartDT.Columns.Add("Qua
objCartDT.Columns.Add("Per
objCartDT.Columns.Add("Use
objCartDT.Columns.Add("Beg
objCartDT.Columns.Add("End
objCartDT.Columns.Add("Nam
objCartDT.Columns.Add("Spo
objCartDT.Columns.Add("Ema
objCartDT.Columns.Add("Cit
objCartDT.Columns.Add("Sta
objCartDT.Columns.Add("Zip
Session["Cart"] = objCartDT;
}
how are you trying to get it working in other pages? You're not trying to use the same table, aren't you?
like in the other page, you would do this in the page_load
if( Session[ "Cart" ] != null )
anotherPageTable = ( DataTable )Session[ "Cart" ];
and of course your anotherPageTable should be declared as well:
DataTable anotherPageTable = new DataTable();
regs,
yurich
like in the other page, you would do this in the page_load
if( Session[ "Cart" ] != null )
anotherPageTable = ( DataTable )Session[ "Cart" ];
and of course your anotherPageTable should be declared as well:
DataTable anotherPageTable = new DataTable();
regs,
yurich
ASKER
If I create a new table, how do I get the data that was stored in the first one?
This is the code I have for the 2nd page. I get the same thing whether or not the line is commented out.
private void Page_Load(object sender, System.EventArgs e)
{
//DataTable objCartDT = (DataTable) Session["Cart"];
this.DataGrid1.DataSource = "objCartDT";
this.DataGrid1.DataBind();
}
This is what I'm getting in DataGrid1 when the code is run. I know there is data in the dataTable before moving to this page, because I'm displaying it on the original page.
Item
o
b
j
C
a
r
t
D
T
This is the code I have for the 2nd page. I get the same thing whether or not the line is commented out.
private void Page_Load(object sender, System.EventArgs e)
{
//DataTable objCartDT = (DataTable) Session["Cart"];
this.DataGrid1.DataSource = "objCartDT";
this.DataGrid1.DataBind();
}
This is what I'm getting in DataGrid1 when the code is run. I know there is data in the dataTable before moving to this page, because I'm displaying it on the original page.
Item
o
b
j
C
a
r
t
D
T
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, it's working now. I have this question up in the C# area too. If you'll post a comment, I'll give you those points too.
https://www.experts-exchange.com/questions/21733741/Retaining-Values-from-data-table-across-pages-in-ASP-NET.html#15936616
https://www.experts-exchange.com/questions/21733741/Retaining-Values-from-data-table-across-pages-in-ASP-NET.html#15936616
Pemberley1,
Ha Ha, I wish I could, but I believe that is against site rules. You'll have to go through the (very quick) process of posting a question in Community Support to delete the question in C#:
https://www.experts-exchange.com/Community_Support/
Thanks for the offer, though... ;) Glad you got it working. Take care.
-- Jason
Ha Ha, I wish I could, but I believe that is against site rules. You'll have to go through the (very quick) process of posting a question in Community Support to delete the question in C#:
https://www.experts-exchange.com/Community_Support/
Thanks for the offer, though... ;) Glad you got it working. Take care.
-- Jason
ASKER
Oh oops, didn't know that.
Thanks again.
Thanks again.
-- Jason