0

我创建了一个使用 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;

现在我想将此会话数据存储在数据表中。我该怎么做?

我正在使用此代码

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

1 回答 1

2

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
于 2012-11-14T07:30:53.593 回答