Hi,
I have bound the data to the web data grid. The grid has checkbox and dropdown columns. When I update the record in the grid and try to save, I recieve the below error message. Kindly let me know on how to fix the issue.
Requested record cannot be found by key.Common Causes:- The data key field(s) is being edited causing the record not to be found when trying to update.- Not rebinding the grid when there are updates to be committed (assuming DataViewState is disabled in this case) – for instance if the grid is bound on !IsPostback.- While a user is editing a particular row in the grid, another user deletes the record from the database. On posting back, the grid is rebound to the updated datasource which no longer contains the record, resulting in the exception.- Changing completely or filtering the grid’s datasource before all CRUD operations are carried out – this is quite common when using other controls on the page to do this . This is partly due to the fact that control events such as SelectedIndexChanged for a dropdownlist or Click for a button fire before the updating events. Changing the grid’s source should in such cases be delayed for later (PreRender).
Hello,
I would need a running sample that reproduces the issue, or at least a code snippet in order to be able to provide more insight.
I can suggest you to look through the common causes mentioned in the exception and see if you can find something in your code.
In the sample aspx page we have below code
-----------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridWithKey.aspx.cs" Inherits="GridWithKey" %>
<%@ Register assembly="Infragistics4.Web.v14.1, Version=14.1.20141.1015, Culture=neutral, PublicKeyToken=7DD5C3163F2CD0CB" namespace="Infragistics.Web.UI.GridControls" tagprefix="ig" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ig:WebDataGrid ID="WebDataGrid1" runat="server" Height="350px" Width="400px" >
<Behaviors>
<ig:EditingCore>
<ig:CellEditing>
</ig:CellEditing>
</Behaviors>
</ig:EditingCore>
</ig:WebDataGrid>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btnsave" runat="server" onclick="btnsave_Click" Text="Save" />
</form>
</body>
</html>
----------------------------------------------------------------------------------------
In the GridWithKey.aspx.cs, we have below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class GridWithKey : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Columns.Add("name");
dt.Columns.Add("city");
dt.Columns.Add("id");
dr[0] = "RAJ";
dr[1] = "Delhi";
dr[2] = 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "JOHN";
dr[1] = "NEWYORK";
dr[2] = 2;
dt.PrimaryKey = new DataColumn[] { dt.Columns[2] };
WebDataGrid1.DataSource = dt;
WebDataGrid1.DataBind();
WebDataGrid1.DataKeyFields = "id";
}
protected void btnsave_Click(object sender, EventArgs e)
When I edit one of the rows in the grid and try to click Save button I get the below mentioned error.
We need to loop the rows server side and save them to the database on clicking save button.
Kindly let me know on the right method to use when using Dataset as Datasource.
Server Error in '/eFACiLiTYNew' Application.
It will help if you can provide an example that duplicates the issue or at least the relevant code as editing will still work when you set the DataSource in the page Load event.
Let me know if you have any questions with this matter.
Alan,
If we bind grid in Postback then we can not get edited values.
For example, we have edited name from "A" to "B" in particular cell of webdatagrid and click "save" button
In page load grid bind again with name "A". changed /edited value missing.
Please suggest on this...
I enabled the DataViewState on the WebDataGrid and the issue was solved. Thanks for all the suggestions.
Hi sucheendarnath,
Did you try those suggestions, did they work for you? Please let us know.
Manh,
I wouldn't expect setting the active cell to null to have any impact on this behavior as it wouldn't prevent the update from being done on the row so the row would still need to be found in the data source. From the code provided most likely my suggestion of setting the data source on post back will resolve the issue. If it doesn't then I would need more details from sucheendarnath.
Let me know if you have any questions.