Dataset Accept change is not working
-
I am trying to copy one dataset to another and calling acceptchanges to update database but it is not working although not giving any error. here is the code. SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM " + DestinationTableName, consql); DataSet dsDest = new DataSet(); dsDest.CaseSensitive = false; da2.Fill(dsDest, DestinationTableName); dsDest.Tables[0].TableName = DestinationTableName; DataTable dt = new DataTable(); dt.TableName = DestinationTableName; foreach (DataRow dr in dsSource.Tables[0].Rows) { DataRow drdata= dsDest.Tables[0].NewRow(); foreach (DataColumn dc in dsSource.Tables[0].Columns) { drdata[dc.ColumnName] = dr[dc.ColumnName]; } dsDest.Tables[0].Rows.Add(drdata); dsDest.AcceptChanges(); da2.Update(dsDest.Tables[0]); } Whats wrong with above code..?
-
I am trying to copy one dataset to another and calling acceptchanges to update database but it is not working although not giving any error. here is the code. SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM " + DestinationTableName, consql); DataSet dsDest = new DataSet(); dsDest.CaseSensitive = false; da2.Fill(dsDest, DestinationTableName); dsDest.Tables[0].TableName = DestinationTableName; DataTable dt = new DataTable(); dt.TableName = DestinationTableName; foreach (DataRow dr in dsSource.Tables[0].Rows) { DataRow drdata= dsDest.Tables[0].NewRow(); foreach (DataColumn dc in dsSource.Tables[0].Columns) { drdata[dc.ColumnName] = dr[dc.ColumnName]; } dsDest.Tables[0].Rows.Add(drdata); dsDest.AcceptChanges(); da2.Update(dsDest.Tables[0]); } Whats wrong with above code..?
You are calling methods in wrong sequence. You need to first call Update() of DataAdapter and then you can call AcceptChanges() of your dataset to reflect changes to database.
dsDest.Tables\[0\].Rows.Add(drdata); da2.Update(dsDest.Tables\[0\]); dsDest.AcceptChanges();
For further help refer following links. http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx[^] http://www.akadia.com/services/dotnet_update_form.html[^] Hope this will help!
Jinal Desai - LIVE Experience is mother of sage....