I am trying to delete a selected gridview row using LINQ (No LINQDataSource).
When the selection is changed, the detailsview binding is changed also. I can add a new
The way i fixed it was: First I update the database, then I set the new values for the grid as
e.Keys["ColumnOne"] ="new value"
e.Keys["ColumnTwo"] ="new value"
All this was done under the GridView_RowUpdating
event.
Also - if you're calling the selecting method for the linqdatasource and setting e.result manually, make sure you're including any foreignkey values as well.
Nothing else worked for me but this.
I had a similar problem and although deleting and re-adding the DBML table/class helped some users, for me it was a bit different since I'm using WCF with detached entity and a ListView on the client.
If I used the .Attach(entity) it failed - "Row not found or changed" But when using .Attach(entity, original) it works every time
public void DeleteTask(Task task)
{
TwoDooDataContext db = new TwoDooDataContext();
db.Tasks.Attach(task,GetTaskByID(task.ID));
db.Tasks.DeleteOnSubmit(task);
db.SubmitChanges();
}
I just wanted to add my scenario for anybody who may have this issue.
We use a custom T4 against our Linq to SQL dbml. We basically just modified the original get/set of string properties to automatically trim and set null.
get { return _OfficiantNameMiddle.GetValueOrNull(); }
set
{
value = value.GetValueOrNull();
if (_OfficiantNameMiddle != value)
{
_IsDirty = true;
OnOfficiantNameMiddleChanging(value);
SendPropertyChanging("OfficiantNameMiddle");
_OfficiantNameMiddle = value;
SendPropertyChanged("OfficiantNameMiddle");
OnOfficiantNameMiddleChanged();
}
}
Legacy data in our database had some leading/trailing spaces so any concurrency check on these columns failed to result in a match (it was comparing the trimmed value against the non-trimmed database value). It was really easy to profile SQL, grab the SQL and start commenting out the items in the WHERE clause until it started returning a row during the concurrency check.
Luckily, we have a LastUpdatedOn field in our tables that is automatically set via OnValidate(System.Data.Linq.ChangeAction).
partial void OnValidate(System.Data.Linq.ChangeAction action)
{
if (action == System.Data.Linq.ChangeAction.Insert)
{
CreatedBy = CurrentUserID;
CreatedOn = DateTime.Now;
LastUpdatedBy = CreatedBy;
LastUpdatedOn = CreatedOn;
}
else if (action == System.Data.Linq.ChangeAction.Update)
{
LastUpdatedBy = CurrentUserID;
LastUpdatedOn = DateTime.Now;
}
}
In order to bypass the problem, we just set the concurrency check to Never on all columns except the Primary Key columns and LastUpdatedOn column. This worked for us.
For me it was an enum column (mapped to a varchar) which caused the problem, so I had to pass update check to never.
This is just a case of mismatched column definitions. Just remove the table from .dbml
and re-add. Make sure to change auto generate value property = true
for columns that have auto generated data such as Primary Keys or datetime columns.