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:
My button has the following code to look through the table:
Code
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?
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Blocked AccountRank #403 · 6.2K reputationPosted Nov 22, 2010, 8:49 PM EST
0
HI Lex,
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
0
Sam HobbsRank #107 · 20.5K reputationPosted Nov 22, 2010, 11:14 PM EST
0
Be sure to use try/catch so you don't miss something critical. If that does not determine the problem, then use the debugger to single-step through the code and ensure that everything is working as you expect.
I first thought it was the position of where I was defining the table. But a friend of my pointed me in the right direction.
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.
0
Sam HobbsRank #107 · 20.5K reputationPosted Nov 22, 2010, 11:40 PM EST
0
It is likely that use of try/catch will eliminate a few embarrassing situations such as this.
Blocked AccountRank #403 · 6.2K reputationPosted Nov 22, 2010, 8:49 PM EST
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
Sam HobbsRank #107 · 20.5K reputationPosted Nov 22, 2010, 11:14 PM EST
Lex15 reputationPosted Nov 22, 2010, 11:25 PM EST
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 HobbsRank #107 · 20.5K reputationPosted Nov 22, 2010, 11:40 PM EST