4
<asp:SqlDataSource ID="HopefulDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
    SelectCommand= "SELECT id, regGroupID, amountReceived, other FROM table" 
    UpdateCommand="UPDATE table
                        SET [amountReceived] = @amountReceived
                        WHERE [regGroupID] = @regGroupID">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlCourses" Name="ddlSelectedCourse" PropertyName="SelectedValue" Type="String" />
    </SelectParameters>

    <UpdateParameters>
        <asp:Parameter Name="regGroupID"        Type="Int32" />
        <asp:Parameter Name="amountReceived"    Type="Decimal" />

        other parameters

        <asp:Parameter Name="id"                Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

The above works when I change "WHERE [regGroupID] = @regGroupID" to either

WHERE [id] = @id

or

WHERE [regGroupID] = 2

2
  • How are you setting the value of your regGroupID parameter? Commented Feb 26, 2013 at 14:27
  • Through a GridView row edit Commented Feb 26, 2013 at 14:28

1 Answer 1

5

You need to add "regGroupID" to the DataKeyNames collection in your GridView declaration. Something like this:

<asp:GridView ID="yourGridViewId" DataKeyNames="regGroupID" ... >
    ...
</asp:GridView>

See the DataKeyNames documentation:

You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete.

Note: it's been a while since I used this, so you might need to include both the primary key, and your other key field. Like this:

DataKeyNames="id,regGroupID"
Sign up to request clarification or add additional context in comments.

2 Comments

You! You're a good person. Thank you so much for this! It defaulted to having just id, but adding regGoupID ended hours and hours of work! I both hate and love when one little thing fixes a HUGE problem! Cheers!
@statue I totally know what you mean. And you're welcome, I'm glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.