troubleshooting Question

Pass datatable between asp.net pages

Avatar of Pemberley1
Pemberley1 asked on
ASP.NET
7 Comments1 Solution1987 ViewsLast Modified:
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("CartID", typeof(Int32));
                    objCartDT.Columns["CartID"].AutoIncrement = true;
                    objCartDT.Columns["CartID"].AutoIncrementSeed = 1;
                    objCartDT.Columns.Add("WageType", typeof(string)); //lgart
                    objCartDT.Columns.Add("TotalCost", typeof(Decimal));
                    objCartDT.Columns.Add("Deduction", typeof(Decimal)); //Betrg
                    objCartDT.Columns.Add("Phone", typeof(string));
                    objCartDT.Columns.Add("MemName", typeof(string));
                    objCartDT.Columns.Add("Addr", typeof(string));
                    objCartDT.Columns.Add("Quantity", typeof(Int32)); //number of children
                    objCartDT.Columns.Add("Pernr", typeof(string));
                    objCartDT.Columns.Add("User", typeof(string)); //userid
                    objCartDT.Columns.Add("BegDate", typeof(string)); //begin of deduction period
                    objCartDT.Columns.Add("Endda", typeof(string)); // end of deduction period
                    objCartDT.Columns.Add("Name", typeof(string)); //name on membership
                    objCartDT.Columns.Add("Spouse", typeof(string)); //spouse on membership
                    objCartDT.Columns.Add("Email", typeof(string));
                    objCartDT.Columns.Add("City", typeof(string));
                    objCartDT.Columns.Add("State", typeof(string));
                    objCartDT.Columns.Add("Zip", typeof(string));
                                                                Session["Cart"] = objCartDT;

               
               }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros