Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
after assign a row values of datatabe to a new data row object , any changes in this object affects on the data table , Why ?

have a look at this code.

C#
DataRow dr = mydataset.Tables["NewTable"].Rows[0];
dr["Name"] = TextBox2.Text;


Now if you try to debug your application you will find that the original data table ["NewTable"] has the new value "hello" instead of the old value Although we didn't return back the above data row to data table (we didn't save it )

I Just need a clarification and also need to know & understand what actually happen when i create an Object of a data row .
Posted
Comments
Tomas Takac 22-Nov-14 7:20am    
Of course the "NewTable" has the value, you just assigned it. If you want the original value use the overload which takes DataRowVersion[^] like this: dr["Name", DataRowVersion.original].
Hercal 22-Nov-14 7:37am    
Thanks Tomas .

1 solution

OK, this is going to get interesting, because it's kinda fundamental to how .NET works.

DataRow is a Class - which means it's what's called a "Reference type" rather than a "Value type" as (for example) integers are.
When you create an integer variable and assign it a value:
C#
int x = 5;
int y = x;
You crate a new variable and copy the value into it: so if you then do this:
C#
x = x + 10;
Console.WriteLine("{0}:{1}", x, y);
It will print:
15:5
Because y got a copy of the content of x, not a reference to it. This is what you expect!

Reference types are different:
C#
public class MyClass
   {
   public int Val;
   }
...
MyClass x = new MyClass();
x.Val = 5;
MyClass y = x;

x.Val = x.Val + 10;
Console.WriteLine("{0}:{1}", x.Val, y.Val)
This time it prints
15,15
Because y got a copy of the content of x, which was a reference to the actual instance. So when you changed the Val via the x variable, it also changed the Val of the instance that y referred to because they are both referencing the same instance!

The same thing is happening in your code:
C#
DataRow dr = mydataset.Tables["NewTable"].Rows[0];
Createa a new variable called dr, and assigns a copy of the reference to the Table row to it - they both refer to the same area of memory, the same instance of a Row.
So then:
C#
dr["Name"] = TextBox2.Text;
Changes the one and only instance - which affects the instance in the table because it's the same one!

Does that make sense?

[edit]
There is a more detailed description of the difference here: Using struct and class - what's that all about?[^]
[/edit]
 
Share this answer
 
v2
Comments
Hercal 22-Nov-14 7:35am    
Good , Perfect answer , but what if i need a copy of data table's row that i will use in another case , and i don't need the original table to be affected , what should i do ?
OriginalGriff 22-Nov-14 8:02am    
Create a new row, with the same schema:
DataRow dr = mydataset.Tables["NewTable"].NewRow();
Then, copy it's objects:
dr.ItemArray = mydataset.Tables["NewTable"].Rows[0].ItemArray.Clone() as object[];
Then, you can set the new data!
dr["Name"] = TextBox2.Text;

Note: the Clone method does a shallow copy, but in this case it should work fine.
Hercal 22-Nov-14 8:54am    
thank you so much :)
OriginalGriff 22-Nov-14 11:20am    
You're welcome!

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