Hi I am trying to use a gridview to display information, providing update/delete functions. But I'm keeping getting error messages.

This is the code:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <asp:GridView ID="GridView2" runat="server" AllowSorting="True"  
        AutoGenerateColumns="False" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" DataSourceID="SqlDataSource1" DataKeyNames="name, number">
        <Columns>
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="number" HeaderText="number" 
                SortExpression="number" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:pbdbConnectionString %>" 

        SelectCommand="SELECT * FROM [contact]" 
        ConflictDetection="CompareAllValues" 
        
        InsertCommand="INSERT INTO [contact] ([name], [number]) VALUES (@name, @number)" 
        DeleteCommand="DELETE FROM [contact] WHERE name=@name AND number=@number"
        UpdateCommand="UPDATE [contact] SET name=@name, number=@number WHERE name=@name AND number=@number"
        
        OldValuesParameterFormatString="original_{0}" >
        <DeleteParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="number" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="number" Type="String" />
        </InsertParameters>


        </asp:SqlDataSource>

</asp:Content>

Update error message:

You have specified that your update command compares all values on SqlDataSource 'SqlDataSource1', but the dictionary passed in for oldValues is empty. Pass in a valid dictionary for update or change your mode to OverwriteChanges.

Delete error message:

You have specified that your delete command compares all values on SqlDataSource 'SqlDataSource1', but the dictionary passed in for values is empty. Pass in a valid dictionary for delete or change your mode to OverwriteChanges.

Please advise. Thank you.

OK now when I change "ConflictDetection="CompareAllValues" to ConflictDetection="OverwriteChanges", I no longer get the error, but delete and update doesn't seem to work at all, which means, after clicking delete, the data is still there.

Do not check the "Use optimistic concurrency" while configuring a SqlDatasource.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.