tags:

views:

111

answers:

3

----> I have datatable which is passing to another page in session variable.

----> Now on another page i take the session variable into datatable.

datatable ds_table = new datatable();
ds_table = (datatable)session["table_value"]; 

----> so problem, is that , when i filtering some rows from ds_table . that taking effect in the session variable. if some rows deleted from ds_table. then it is also deleted from session variable.

----> so, anyone tell me why is this going to happene?

help me. its necessary.

A: 

Well, if session["table_value"] points to the datatable, and you make assign its value (the datatable) to another variable, and THEN MAKE CHANGES to that variable, the changes will be relfected in the datatable and thus you'll see the effect you illustrated.

----- Not always, but in the case you illustrated (so is datatable a shared resource?)

Mr-sk
is it a solution or advise?
Sikender
A: 

Sounds like you want to make a copy of the DataTable. Try this:

ds_table = ((datatable)session["table_value"]).Copy(); 

This will be bad if the DataTable is large, so bear that in mind. That said using Session for a large DataTable sounds like a bad idea anyway!

Of course whether that works or not depends on where you are changing its state (I've made some big assumptions). Perhaps describe in more detail what you are doing, and you will get more help (e.g. a code example).

RichardOD
i tried but it making problem. same as i have.
Sikender
OK. Please post some more code of what you are doing exactly.
RichardOD
A: 

I would suggest that you copy the datatable before placing it in session. Then, it won't be affected by any changes you make later to the original datatable.

All you have to do is declare a new DataTable variable and copy it:

private void CopyDataTable(DataTable table){
    // Create an object variable for the copy.
    DataTable copyDataTable;
    copyDataTable = table.Copy();

    // Insert code to work with the copy.
 }

Then you can put copyDataTable into session.

DOK
problem is not making session variable. problem is that taking value into datatable from the session variable. and then i making changes to that datatable at another page. it takes changes to session variable.also.
Sikender
When you get to the other page and want to use the DataTable from session, create a copy of the DataTable that is in session. then, you can alter the copy without having any effect on the original DataTable that remains in session.
DOK
i will try as per you said . but its not that kind of effective. man.result is same as what i got before
Sikender
You just need to make sure that you never change the instance of the DataTable that you put into session. Read it out of session, then copy it nto a new instance, and only change that instance.
DOK