Friday 25 July 2008

LinqDataSource and Could not find a row that matches the given keys in the original values stored in ViewState.

Today I was working on something quickly where I used a LinqDataSource, to keep things simple I attached a simple list view to it and had insert, update and delete operations.


Insert operations seemed to work OK however when I came to do an update to an existing item it crashed out

   Could not find a row that matches the given keys in the original values stored in ViewState.

Initially I thought that maybe some databinding had gone astray but this wasn't the issue. As I hadn't used design view but source view to create the ListView had missed off the DataKeyNames attribute, this meant that upon update the LinqDataSource was trying to match all my values against the database, of course it couldnt do this. By specifying the DataKeyName to match against, usually your Primary Key if using LinqtoSql, not only do you limit how much matching and work your LinqDataSource has to do, but also you'll find you can update :D


So quick tip, if you get "Could not find a row that matches the given keys in the original values stored in ViewState." double check your ListView, FormView, GridView etc has the DataKeyNames set.

Monday 21 July 2008

System.Web.Query.Dynamic.ParseException: Digit expected

Today I wrote a quick piece of code using Linq to Sql that would pull out some records from a table given a provided where clause
To make my life easy, I used a LinqDataSource within a web form and then databound a form view to it, this was only a quick prototype of an idea so I felt these were appropriate.


The where clause for my LinqDataSource was being assigned programatically and consisted of a GUID value from a user object I had knocked together. Upon compiling and running the code though I got an odd error: System.Web.Query.Dynamic.ParseException: Digit expected


First thing to check was that the value I was giving the where clause was actually an GUID value, debugging showed this fine, I next looked at putting a hard coded GUID where clause in to see if it was some weirdness caused by my user object, but this wasn't the case. I finally googled the error message, and found 1 meesly result which pointed to the MSDN forums.


Here someone posted that you can convert to various types and to try explicitly converting to a GUID value


   lds.Where = string.Format("user_id == Guid({0})", user_id);

This worked fine, and it made sense too, as when you declare where parameters on the linqdatasource control you normally define its type, as I wasn't doing this nor programtically it was type casting by itself.

Monday 14 July 2008

Build failed due to validation errors in. dbml

OK so I got in today to find that I was unable to build my current project, I got the following

Error 1 Build failed due to validation errors in C:\xxx\DB.dbml. Open the file and resolve the issues in the Error List, then try rebuilding the project. C:\xxx\DB.dbml 2

I opened the projects dbml files to find that these files were fine, no errors or typos, the project was just dead. a little bit of Googling lead me to find that its a problem with Visual Studio 2008 not loading an assembly correctly, in particular the one with a GUID of 8D8529D3-625D-4496-8354-3DAD630ECC1B

In order to load the assembly correctly you need to get Visual Studio to reset it's packages, to do this:

  1. Open a new command prompt
  2. Navigate to C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
  3. Run Visual Sudio with the /resetskippkgs argument: devenv /resetskippkgs

You should now find that your project opens and builds correctly! I'm hoping that maybe SP1 of Visual Studio will correct this, if not SP2 maybe...

Update: SP1 Didn't fix this :( Also worth noting if you are running a 64 bit OS then the visual studio folder path is C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE