But there are a few gotchas you may need help with. Here's one:
OK, when Googling the above error, you'll find a bunch of people saying - just add the DataKeyNames property to your formview! Well that doesn't always fix this problem!
Yes, there is a more insidious cause for this error... mismatching case in the spelling of the id field.
You see, the LINQ designer capitalises all instances of the string "id" which occur at the end of a name to "ID". This causes issues because you, the developer, merrily put the name of the database ID column into the DataKeyNames property as you wrote it in the database definition.
For example:
<asp:formview datakeynames="RecordId" datasourceid="MyDataSource" id="FormView1" onitemupdating="FormView1_Updating" runat="server">
</asp:formview>
, not realising that the LINQ designer has named your primary key field "RecordId" as "RecordID"
Yes! It's as simple as that.
So next time you get the "Could not find a row that matches the given keys in the original values stored in ViewState" error, open up your DBML file and double-check the name of your primary key field.
By the way, this applies to insert and delete operations too.
4 comments:
This does not work !!!
Care to elaborate?
Thank you! Thank you! Thank you!! I've checked the DataKeyNames dozens of times. But didn't notice that, in this particular table (created by someone else), the primary key was named "ID" instead of "Id".
You're welcome Ana - glad it helped you!
Post a Comment