I’m working in ASP.NET ( beginner ) and I tried to make update query using the code below. I try the same code to get date from database and it work fine, but only don’t work with update query.
Note: I use LINQ
Dim updateCust = (From cust In db.Customers
Where cust.CustomerID = "JILLF").ToList()(0)
updateCust.ContactName = "Jill Shrader"
Try
db.SubmitChanges()
Catch
' Handle exception.
End Try
RefreshData()
I got this code from : http://msdn.microsoft.com/en-us/library/bb907191.aspx
Should I change something in my SQL server 2008 so I could use update option ?
The problem is that you are converting your results into a
Listand then take first element. You’ll get the data but it is not bound to database any more. I believe something like this would work:EDIT
Although
FirstOrDefault()would work fine here you may want to useSingleOrDefault()which would throw an exception if there were more than one record with the sameCustomerID(or broadly, more records returned). I assume that this column is a primary key column (it looks like it) and that guarantees row uniqueness.