views:

19

answers:

1

When I try to update Linqdatsource binded to gridview get following error "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."

Note :- I have specified DataKeyNames in gridview

Please find the html and code as well

<asp:GridView ID="TaskGridView" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="taskid,statusid,taskdescription" DataSourceID="GridDataSource" 
      onrowcreated="TaskGridView_RowCreated">
    <Columns>
        <asp:TemplateField HeaderText="taskid" InsertVisible="False" 
            SortExpression="taskid">
            <ItemTemplate>
                <asp:Label ID="TaskId" runat="server" Text='<%# Bind("taskid") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="taskdescription" 
            SortExpression="taskdescription">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("taskdescription") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="TaskDesc" runat="server" Text='<%# Bind("taskdescription") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
      <asp:TemplateField HeaderText="Status">
        <ItemTemplate>
            <asp:DropDownList runat="server" ID="ddStatus" DataSourceID="DropDownDataSource" DataValueField="statusid" SelectedValue="<%# Bind('Statusid') %>" DataTextField="statusdescription" ></asp:DropDownList>
        </ItemTemplate> 
      </asp:TemplateField>       
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="GridDataSource" runat="server" 
    ContextTypeName="DailyTask.DailyTaskDBDataContext" TableName="tbl_tasks" 
      EnableUpdate="True">
</asp:LinqDataSource>
<asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" 
Text="Update" />
<asp:LinqDataSource ID="DropDownDataSource" runat="server" 
    ContextTypeName="DailyTask.DailyTaskDBDataContext" TableName="tbl_status">
</asp:LinqDataSource>

CODE BEHIND

protected void btnUpdate_Click(object sender, EventArgs e) { ListDictionary keyValues = new ListDictionary(); ListDictionary newValues = new ListDictionary(); ListDictionary oldValues = new ListDictionary(); try { keyValues.Add("taskid", ((Label)TaskGridView.Rows[0].FindControl("TaskId")).Text); oldValues.Add("taskdescription", ((Label)TaskGridView.Rows[0].FindControl("TaskDesc")).Text); newValues.Add("taskdescription", "New Taskk"); GridDataSource.Update(keyValues, newValues, oldValues); } catch (Exception ex) { Response.Write(ex.Message); } }

A: 

I got the problem it was in the code

I just have to use int.Parse() here because taskid is primary key

 keyValues.Add("taskid", int.Parse(((Label)TaskGridView.Rows[0].FindControl("TaskId")).Text));
Raj Kumar