Link to home
Start Free TrialLog in
Avatar of Pemberley1
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("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;

               
               }
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Once you've stored the DataTable in that Session["Cart"] object, you should be able to access it from any page within the application.  Are you getting an error when attempting to access it?  Are you sure it's getting populated?

-- Jason
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
Avatar of Pemberley1
Pemberley1

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
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
Oh oops, didn't know that.
Thanks again.