none
LinqDataSourceView and Could not find a row that matches the given keys. RRS feed

  • Question

  •  

    It might just be that it's late and my brain has come unwired, but I cannot figure out why I am unable to perform a very basic update when using a FormView with the LinqDataSource.

     

    I have the LinqDataSource getting record(s) from my database through the data context that is created from the LinqToSql project. The table in question has the 'UserName' field set as a primary key and this is how I am fetching the record.

     

    The FormView is displaying the record, I can change to the edit view, however it is unable to save the changes. I get the following exception:

     

    Code Snippet
    Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation.
     
    [HttpException (0x80004005): Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation.]
       System.Web.UI.WebControls.LinqDataSourceView.GetOriginalValues(IDictionary keys) +926
       System.Web.UI.WebControls.LinqDataSourceView.BuildUpdateDataObjects(Object table, IDictionary keys, IDictionary values, IDictionary oldValues) +102
       System.Web.UI.WebControls.LinqDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +87
       System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +78
       System.Web.UI.WebControls.FormView.HandleUpdate(String commandArg, Boolean causesValidation) +1150
       System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +440
       System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e) +95
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
       System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e) +109
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
    

     

    Has anyone else encountered this problem?

    • Moved by Noam Ben-Ami - MSFT1 Friday, January 22, 2010 8:19 PM (From:ADO.NET Entity Framework and LINQ to Entities)
    Friday, August 31, 2007 8:41 AM

Answers

  • I believe this is a bug related to how LINQ handles rows with null values. I opened a bug for it.

     

    Bug #296161

    Friday, August 31, 2007 11:12 PM

All replies

  • I believe this is a bug related to how LINQ handles rows with null values. I opened a bug for it.

     

    Bug #296161

    Friday, August 31, 2007 11:12 PM
  • So is there a workaround? It seems to affect inserts as well? I've got an autopulating key but my inserts crash when there's no value in the field, even though it gets ignored?
    Is there a way to see the SQL on Linq Datasources?
    Thanks,
    Michael
    ---
    http://www.radiantdays.com
    Thursday, April 17, 2008 4:34 PM
  • Do you have a link to the bug? I can't find it on Connect.

    Saturday, April 19, 2008 11:13 PM
  • This is way late for the original post, but did you try setting the DataKeyNames property on the FormView?
    • Proposed as answer by NoHuman Monday, January 26, 2009 10:40 AM
    Saturday, May 24, 2008 6:43 PM
  • I encountered the same problem when attempting do delete a row from gridview.  Per the above advice, I set the datakeynames to the column in my table that held the key.

    In my case, I had forgotten to set a primary key in the table prior to building the DBML.  I then linked my LINQ item to the keyless table.  When I realized my error, I updated the table, deleted it from the DBML and re-added it.  Apparently however, since it had no key when I added the LINQ to my aspx page, it does not retroactively populate the datakeyname (makes sense I guess).    Adding this key manually fixed the problem.  Thanks.
    • Proposed as answer by Vinylis Friday, April 3, 2009 1:47 PM
    Sunday, October 26, 2008 5:34 PM
  • I had the same problem today. The way to fix this problem is, first of all when you are getting the columns by linq data source you have to get all the columns. not some of them so check *. And then be sure that you enabled update or delete which you may prefer, and then goto properties of the gridview, formview.There is Datakey Names. select it and there will be a menu which lets you to add columns, be sure to add only Primary key of your table, ,then you are done.

    IF you are not seeing a menu lets you to choose and you see an empty place asking you to insert string, then recheck that you are getting all columns from Linq Datasource, you dont have to display all of them but you have to receive them in order to delete.

    Friday, April 3, 2009 1:52 PM
  • I believe this is a bug related to how LINQ handles rows with null values. I opened a bug for it.

     

    Bug #296161

    I've encountered a similar problem whilst trying out a tutorial on Linq.  Basically, it is being displayed for DELETE and UPDATE operations. 
    Any news on this bug?  Are there any workarounds for this issue?

    NOTE 1: I can't paste the code due to a limitation
    Monday, May 4, 2009 11:31 AM
  • Excuse my late reply :)

    What is 'Connect'?
    Monday, May 4, 2009 11:35 AM
  • I got the same error using a gridview to bind to a linq datacontext object. (updating and inserting)

    SOLUTION is to set the DataKeyNames property of the Gridview to your table/entity's primary key field(s).
    da mvp
    • Proposed as answer by _ca_ Monday, April 16, 2012 6:22 PM
    Wednesday, July 22, 2009 7:03 AM
  • This post (above) was helpful. Thanks
    I also added primary key in the designer of DBML file.
    +
    In the next post - DataKeyNames - also need Primary Key to Gridview properties.
    Friday, August 7, 2009 7:28 PM
  • This post was helpful. Thanks
    Friday, August 7, 2009 7:28 PM
  • This helps a lot! It does work
    Tuesday, October 27, 2009 9:48 AM
  • DataKeyNames was part of my problem, but did not fix the issue.

    My particular problem was resolved by adding 

    StoreOriginalValuesInViewState="True"

    to the LinqDataSource.
    Tuesday, October 27, 2009 11:26 AM
  • Make sure that your Master page sets ID in Page_Init and NOT in Page_Load:
    http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=469
    Friday, January 22, 2010 7:26 PM
  • I had the same problem, and in search for a solution I luckily reached to this page. I got a hint from billg_c__asp35_VS2008's answer. The problem in my case was related to an old table in my dbml model. I later made some changes to my database after my table has been put into dbml. So I deleted the old table from the dbml schema and re-dragged the table into the dbml model and then it all worked fine. Note that you must declare the primary key(s) of the table in the DataKeyNames property of the GridView control. Thanks billg.
    Sunday, January 16, 2011 5:35 PM
  • If you do not specify every column value ir - respective of it is changed or not, ldsGeneric.Update(...) method will give the error LinqDataSourceView and Could not find a row that matches the given keys.

    e.g Your Product table has columns ProductID (primary key), ProductName, ProductType

     

    ListDictionary keyValues = new ListDictionary();
        ListDictionary newValues = new ListDictionary();
        ListDictionary oldValues = new ListDictionary();
        keyValues.Add("ProductID", int.Parse(((Label)DetailsView1.FindControl("IDLabel")).Text));
        oldValues.Add("ProductName", ((Label)DetailsView1.FindControl("NameLabel")).Text);
        newValues.Add("ProductName", "New Product");
        LinqDataSource1.Update(keyValues, newValues, oldValues); // Will give error as ProductType is not added to above collection. After adding this 

     oldValues.Add("ProductType", "Medicine");
     newValues.Add("ProductType", "Medicine");
     LinqDataSource1.Update(keyValues, newValues, oldValues); // Will work fine

    U may refer http://dotnetslackers.com/Community/blogs/bmains/archive/2009/07/27/row-not-found-or-changed-linq-to-sql-error.aspx

    Thursday, March 10, 2011 11:28 AM
  • Also, note that the DataKeyNames property of the GridView control is case sensitive.
    Wednesday, February 8, 2012 10:08 AM
  • This is a definitely bug in Linq, but you can avoid this error

    by settings LinqDataSource.StoreOriginalValueInViewState property value to false.

    Still nothing changes even in .NET version 4.

    Sunday, February 12, 2012 1:54 AM
  • Also, note that the DataKeyNames property of the GridView control is case sensitive.

    This is worth mentioning twice, it's such a simple thing to check first, before traversing the list of other possible causes;  this has caught me out many times!
    Monday, February 13, 2012 2:43 PM
  • Also make sure your primary key is not showing in your gridview. You cannot update that.

    Monday, March 26, 2012 2:09 AM
  • 3 years later ... you are still the man!
    Monday, April 16, 2012 6:23 PM
  • Quite funny,

    My error was solved exactly with the opposite attribute, which actually makes more sense to me:

    StoreOriginalValuesInViewState="false"

    Monday, November 5, 2012 3:07 PM
  • "DataKeyNames was part of my problem, but did not fix the issue.

    My particular problem was resolved by adding 

    StoreOriginalValuesInViewState="True"

    to the LinqDataSource."

    I did both these steps and it helped. Thanks!

    • Edited by planetregin Wednesday, December 26, 2012 5:32 PM
    Wednesday, December 26, 2012 5:31 PM
  • This was my problem as well.

    Thursday, January 2, 2014 4:47 PM