I have a list view with two colums in wpf Customername and Isvalid.I am using linq to sql to get the data from my sql table.when i am trying to update a value to the table i dont see any changes to the table.
Here is my code when i click on the save button:
try
{
CustomersDataContext dataContext = new CustomersDataContext();
Customer customerRow = MyDataGrid.SelectedItem as Customer;
string m = customerRow.CustomerName;
Customer customer = (from p in dataContext.Customers
where p.CustomerName == customerRow.CustomerName
select p).Single();
customer.Isvalid=false;
dataContext.SubmitChanges();
MessageBox.Show("Row Updated Successfully.");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
return;
}
I can see that i am able to query the record based on the customername selected but the value is not updating.
I would be glad if some one can point out where am i missing the logic to update the “ISVALID” value to the data base.
Firstly, where’s your
using(get_datacontext){...}block? You need to dispose ofDataContexts when you are done with them!Anyway…
My guess is that the update statement is generating a where clause that’s far too tight, or just plain wrong.
I would be checking the ‘Update Check’ property of each of the columns in your mapped table in the Linq to Sql designer. The simplest thing to do is to set the primary key column to
Alwaysand set all the others toNever. You can also consider setting them toWhenChanged.The designer’s default behaviour is generally to set it to
Alwaysfor everything; not only does this cause horribleWHEREclauses for updates, but can occasionally also cause problems. Obviously such behaviour is required for proper concurrency checking (i.e. two threads updating the same row); so be aware of that.Oh, thinking of something else – you can also get this behaviour if your table doesn’t have a primary key designated in the designer – make sure one of the columns is.
Finally you can check the SQL being generated when
SubmitChangesis called; by attaching a TextWriter to the DataContext.Log property; or equally IntelliTrace in VS2010 will collect all ADO.Net queries that are run if you start with debugging. This is invaluable in debugging why L2S stuff isn’t working as expected.