user3004443
user3004443

Reputation: 133

Main dataset is getting modified when local dataset is modified

Below is my code.

DataSet ds = new DataSet();
ds = dsComplaints;
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"--Select Complaint--" };
dt.Rows.InsertAt(dr, 0);

dsComplaints is globally declared which has the main data. I am copying it to a local DataSet ds. Then I assign the table from ds to a datatable. I am adding a row(select complaint) in data table. But when the insertion happens, my main dataset i.e. dsComplaints is also added with a row. How can i avoid that. I don't know why this is happening.

Upvotes: 0

Views: 19

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You can use the Copy method to create a new copy of same DataSet instead of using the reference only of the original one to avoid this :

DataSet ds = dsComplaints.Copy();

From MSDN DOCS this is what Copy does:

Copies both the structure and data for this DataSet.

Now you can do whatever is needed with the copy and it would not effect the original version of DataSet.

Upvotes: 1

Related Questions