Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
In my web application assign DB values to a Datatable and it assigned to another datatable object. But when we edit this same change occur in copied datatable object.
 
Datatable dt=new DaTatable()
Datatable dtcopy=new DaTatable()
dt=obj.GetCountry();
dtcopy=dt;
 
dt.columns["NameEng"].ColumnName = "Name";
 

Now in datatble dt's column 'NameEng' change to 'Name .
But same change occur in dtcopy. What is the reason and how can avoid it.
Posted 26-Nov-12 2:11am
Edited 26-Nov-12 2:25am
__TR__28.9K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is because both dtcopy and dt are referencing the same object.
Use DataTable.Copy Method[^] to copy from one datatable to another.
 
Instead of
dtcopy=dt;
 
Use
dtcopy = dt.Copy();
to copy the data table.
 
If you want only the structure of datatable and not the data you can use DataTable.Clone Method[^]
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 505
1 OriginalGriff 258
2 ChintanShukla 195
3 Bhavik_Patel 170
4 Richard Deeming 165
0 Sergey Alexandrovich Kryukov 8,688
1 OriginalGriff 7,237
2 CPallini 2,598
3 Richard MacCutchan 2,080
4 Abhinav S 1,888


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100