Reputation: 1387
I am completely stumped on this issue. I have been searching for a while now and still have no answer for how to solve the problem.
I run a query in which I attempt to set the value on. The problem is that they come up as read only
variables if I do not specify Select New With
. When I specify Select New With
it doesn't update the database. Some of the code is listed below.
My goal is to allow the shooter to update the DataGridView
which would update the database. I was told in another question on StackOverflow that the DataGridView
would not update the database simply by the user inputting data. So I created a update button which runs the code listed below.
Code:
Query using Select New With
allows me to edit variables but won't commit to database:
Dim query = From t In db.Trophies _
Select New With {t.TrophyName, t.WinnerId, t.Name, t.Score, t.Printed}
Query won't let me edit variables. Error message reads "Property 'TrophyName' is 'ReadOnly':
Dim query = From t In db.Trophies _
Select t.TrophyName, t.WinnerId, t.Name, t.Score, t.Printed
Code where I want to update the results:
Dim i as integer = 0
For Each result In query
result.WinnerId = dgvTrophies.Rows(i).Cells(dgvTrophies.Columns("WinnerId").Index).Value
result.Name = dgvTrophies.Rows(i).Cells(dgvTrophies.Columns("Name").Index).Value
result.Score = dgvTrophies.Rows(i).Cells(dgvTrophies.Columns("Score").Index).Value
db.SubmitChanges()
i += 1
Next
Table:
PRIMARY KEY TrophyId int auto-increments
TrophyName varchar(50)
WinnerId char(7) Allow Nulls
Name varchar(20) Allow Nulls
Score int Allow Nulls
Printed bit
Any advice or answers would be greatly appreciated.
EDIT: I've tried setting ObjectTrackingEnabled
to True
but it didn't solve the issue.
EDIT: To get it to do a more "automatic" update, after the user leaves edit mode
on the DataGridView
I call db.SubmitChanges
which updates the database and no longer requires an update button.
Upvotes: 2
Views: 2939
Reputation: 46997
Using Select new with { ...
creates an anonymous object, and changes to that is not submited to the database. You need to work with the linq entities. For Example:
Dim query = From t In db.Trophies _
Select t
Or simply
Dim query = db.Trophies
That will work.
Upvotes: 2