Manish Sasmal
Manish Sasmal

Reputation: 71

Store session value in a datatable in C#

I have created a session which store multiple value using Hashtable.

string productCode = lblProductId.Text;
string mrp = lblPrice.Text;
string quantity = txtQuantity.Text;
Hashtable htPdt = new Hashtable();
htPdt.Add("pdtId", "" + productCode + "");
htPdt.Add("price", "" + mrp + "");
htPdt.Add("quantity", "" + quantity + "");
Session["bag101"] = htPdt;

Now I want to store this session data in a Datatable. How can I do it?

I am using this code

Datatable DtbBag101= (Datatable)Session["bag101"];

Upvotes: 0

Views: 13841

Answers (1)

Adil
Adil

Reputation: 148150

You can not cast hashtable to data table. You need to create datatable assign data to it and then save in session.

DataTable table = new DataTable();
table.Columns.Add("pdtId", typeof(int));
table.Columns.Add("price", typeof(double));
table.Columns.Add("quantity", typeof(double));

table.Rows.Add(1, 2, 3);    
Session["bag101"] = table; // Putting DataTable in Session

DataTable DtbBag101= (DataTable)Session["bag101"]; //Retrieving DataTable from Session

Upvotes: 2

Related Questions