Patrick Gharapetians
Patrick Gharapetians

Reputation: 31

Error updating row in asp.net GridView

I'm trying to understand why i'm getting the following error when executing an update on a GridView's row.

"No key property values were found during an update or delete operation. Check to ensure that key properties specified as binding expressions are available to the data source."

I've already setted the DataKeyNames property of the GridView. The strange is i have 2 LinkButton in the same item template of the GridView: 1 for the update and one for the delete.. the delete one is working, the update one not.

Here is the piece of code in the FrontEnd of the page

<asp:GridView runat="server" ID="gridView" DataSourceID="listDataSource" AllowPaging="false" AllowSorting="false" 
        AutoGenerateColumns="false" AutoGenerateDeleteButton="false" AutoGenerateEditButton="false" AutoGenerateSelectButton="false" OnRowCommand="gridView_RowCommand">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>

                    <div class="valoriFields">
                        <asp:DynamicEntity runat="server" Mode="Insert" />
                    </div>

                    <div class="buttonsFields buttonsFieldsEnabled">
                        <asp:LinkButton runat="server" ID="btnSalva" ClientIDMode="Static" CommandName="Update" CssClass="btnSaveMulti" CausesValidation="true">
                            <span class="icon-save"></span>
                        </asp:LinkButton>

                        <asp:LinkButton runat="server" ID="btnDelete" ClientIDMode="Static" CommandName="Delete" CssClass="btnDeleteMulti" CausesValidation="false">
                            <span class="icon-delete"></span>
                        </asp:LinkButton>
                    </div>

                    <div style="clear:both;"></div>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

<ef:EntityDataSource ID="listDataSource" runat="server" EnableInsert="false" EnableDelete="true" EnableUpdate="true" OnUpdating="listDataSource_Updating" />

And the following is the CodeBehind related to the GridView and his DataSource

listDataSource.WhereParameters.Clear();
listDataSource.EntityTypeFilter = entityType.Name;
listDataSource.ContextType = contextType;
listDataSource.EntitySetName = entityType.Name;
listDataSource.AutoGenerateWhereClause = true;
listDataSource.WhereParameters.Add(parentIdName, DbType.Int32, parentID.ToString());

gridView.SetMetaTable(table, table.GetColumnValuesFromRoute(Context));
gridView.DataKeyNames = new string[] { IdName };

ThankYou

Upvotes: 2

Views: 398

Answers (1)

Patrick Gharapetians
Patrick Gharapetians

Reputation: 31

Ok i found out you have to set and EditIndex to the GridView and so it seems to work. Now i would like to know how it works.. because u can set the EditIndex to 0 and it doesn't matter what row you are trying to edit it works anyway.

I think when you set an EditIndex u set the state of the GridView to "Edit" or something like this..

Know i have a further question.. the first time i clic on the update button it works but after the postback if i try to click and save a different row the old error come back.

Anyone knows how it really works and how to fix this second problem?

THankYou

Upvotes: 1

Related Questions