how to insert single row from datagridview to Database, Linq-to-Sql

721 views Asked by At

I'm trying to insert a row from my datagridview to database, which is newly created. First I use ; db.recordTable.InsertOnSubmit();

however it requires db.SubmitChanges();

to make changes permanent. However db.SubmitChanges(); submits all changes on the gridview to database. But I'm encoding password section on the datagridview therefore submit fails. Is there anyway for me to insert only single row without submitting all changes on the datagridview ?

1

There are 1 answers

2
Ali Adlavaran On

Before submit changes you can track all the changed objects in the DataContext like this:

MyEntity changedEntityToSubmit; // first you need to know what is the entity you need to submit.
List<object> allChangedEntities = new List<object>(myDataContext.GetChangeSet().Inserts);
allChangedEntities.Remove(changedEntityToSubmit);

myDataContext.Refresh(RefreshMode.OverwriteCurrentValues, allChangedEntities);

Then:

myDataContext.recordTable.InsertOnSubmit(rec);
myDataContext.SubmitChanges();