Hi All, I'm using a gridview where I can select multiple lines, I store the keynames of each line into a DataTable, but having trouble using the DataTable when it comes to activating the selected products.
Here is my code to add items to the DataTable:
if (Session["tblSelected"] == null) { DataColumn actID = new DataColumn("actID"); actID.DataType = Type.GetType("System.Int32"); tblSelected.Columns.Add(actID); DataColumn actStatus = new DataColumn("actStatus"); actStatus.DataType = Type.GetType("System.Int32"); tblSelected.Columns.Add(actStatus); DataColumn prodCode = new DataColumn("prodCode"); prodCode.DataType = Type.GetType("System.Int32"); tblSelected.Columns.Add(prodCode); DataColumn[] pCol = new DataColumn[1]; pCol[0] = actID; tblSelected.PrimaryKey = pCol; } else { tblSelected = (DataTable)Session["tblSelected"]; Session["tblSelected"] = null; } DataRow dRow = tblSelected.NewRow(); dRow["actID"] = activationID; dRow["actStatus"] = activationStatus; dRow["prodCode"] = productCode; if (!tblSelected.Rows.Contains(activationID)) { tblSelected.Rows.Add(dRow); } else { tblSelected.Rows.Find(activationID).Delete(); } Session["tblSelected"] = tblSelected;
|
I have defined my DataTable on the top
DataTable tblSelected = new DataTable();
|
My button has the following code to look through the table:
foreach (DataRow row in tblSelected.Rows) { // My code that activates the products.... }
|
Now the problem comes when I go into the foreach loop, it tels me that there are no items in tblSelected.
Anyone has any idea why after I selected a couple of rows, and confirmed they are added to the Datatable that they are not accessable from the button control?
Sam HobbsPosted Nov 23, 2010, 4:40 AM
LexPosted Nov 23, 2010, 4:25 AM
I forgot that I added the table to a session, therefore I was unable to get data from that table, as soon as I pointed the tblSelected to the session it was working.
tblSelected = (DataTable)Session["tblSelected"];
was needed before I checked every entry in the table, when I click the button. A simple mistake which is easy to read over, and even with the debugger hard to spot.
Sam HobbsPosted Nov 23, 2010, 4:14 AM
Blocked AccountPosted Nov 23, 2010, 1:49 AM
Check the position of this line DataTable tblSelected = new DataTable();
i think this line will be in page load, when you click on button it again create the new instance of datatable and all old data get remove.
put it inside the !Page.IsPostBack under Page Load.
and it would be better if you post complete code for understand the problem better.
Thanks