Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, we have a problem with this code:

DataTable DataTable11 = (DataTable)Session["DataTableSession"];
DataTable dtDemo = DataTable11;

if (DataTable11 != null)

The column Id is delete for 2 DataTable var (DataTable11 and dtDemo), how is possible this???

I want to delete the Id Column for DataTable11 and the dtDemo doesn't delete nothing.

Thank you and sorry for my English.

1 solution

You need to go back a few steps and look up the basics on what a variable, a reference, and an instance are.
This code:
DataTable DataTable11 = (DataTable)Session["DataTableSession"];
Creates the DataTable11 variable, and assigns a reference to the DataTable instance from the session to it.
DataTable dtDemo = DataTable11;
This creates a second variable called dtDemo, and copies the reference to the DataTable instance from DataTable11.
It does not copy the table: it copies the reference, so that both variables are referring to the same object. Anything you do via one variable appears to have been done to the other because they are both "talking about" the same item.

Think of a TV as the DataTable, and two remote controls as the variables. Regardless of which remote control you use, they both change the channel on the same TV, because they both refer to the same instance of a television.

If you want to have two separate DataTables so you can change one and not affect the other, then you need to copy the existing instance using the DataTable.Copy[^] method.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900