0

I am trying to use a Session["Shopping_Basket"] to save all my items by serial numbers for later use.

I tryed text string but i had to parse the string to separate all serials and couldn't delete duplicates as easy as with DataTables.

I was able to make it work in a previous version of my software but it refuses to work again on a new version.

 string serialNumber = Request.QueryString["serialNumber"];

           DataTable dataTable_SerialNumber = Session["Shopping_Basket"] as DataTable;
           dataTable_SerialNumber.Rows.Add(serialNumber);
           Session["Shopping_Basket"] = dataTablePanierCommande ;

I am looking for the simplest way to add serial numbers to a Session[""] and be able to Read/Add/Delete them.

The error i am getting the most with this same code that worked before is:

System.NullReferenceException: 'La référence d'objet n'est pas définie à une instance d'un objet.'

This new version of the code didn't work either:

string serialNumber = Request.QueryString["serialNumber"];

HttpContext context = HttpContext.Current;
DataTable dataTable_SerialNumber = context.Session["Shopping_Basket"] as DataTable;                   

dataTable_SerialNumber.Rows.Add(ajouter_GenCod);
context.Session["Shopping_Basket"] = dataTable_SerialNumber ;                    
printBasketContent(context.Session["Shopping_Basket"]);
Rui Ruivo
  • 343
  • 3
  • 12
  • Where did `dataTablePanierCommande` come from? – mjwills Jun 04 '19 at 09:28
  • Instead of saying `Session["Shopping_Basket"] as DataTable;` use a direct cast `(DataTable)Session["Shopping_Basket"];` which would throw exception if casting fails – Rahul Jun 04 '19 at 09:30
  • After you get it out, you need to check if it is `null`. Then, if it is `null`, you need to `new` up a `DataTable` and add it to the `Session`. The problem is you are opening a shopping bag, pulling nothing out of it and then wondering why it isn't acting like a banana. – mjwills Jun 04 '19 at 09:33
  • Something like this: System.Data.DataTable table = new DataTable(); ? – Rui Ruivo Jun 04 '19 at 09:34
  • Well, with `dataTable_SerialNumber` rather than `table`, yes. – mjwills Jun 04 '19 at 09:35

0 Answers0