开发者

Getting error while updating LINQDatasource

When I try to update a LINQ data source bound to a grid view, I get the 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.

I have specified DataKeyNames in the grid view.

Here's the HTML:

<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>

Here's the corresponding code:

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);
    }
}


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜