Specifying the right parameter in SqlDataSource control

I don't recall seeing this before, but after searching a bit in the forums I found that you need to specify the parameters in your WHERE clause of update and delete commands differently for the SqlDataSource control. The following is functional code:

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" DataSourceMode="DataReader"
    ConnectionString="<%$ ConnectionStrings:MyDatabase %>"
    SelectCommand="SELECT CustomerID, Name, City FROM Customers"
    UpdateCommand="UPDATE Customers SET Name = @Name, City = @City WHERE CustomerID = @original_CustomerID"
    DeleteCommand="DELETE FROM Customers WHERE CustomerID = @original_CustomerID">
</asp:SqlDataSource>

<asp:GridView ID="MyGrid" Runat="Server" DataSourceID="SqlDataSource1"
    AutoGenerateEditButton="true"
    AutoGenerateDeleteButton="true" DataKeyNames="CustomerID" />

Notice the red part. Fredrik says this is normal and part of the conflict detection mechanism. I'm down with that, but according to the current version of the docs, this is not default behaviour. I mean, you can't find an example anywhere between GridView and SqlDataSource where this is the case.

I don't have any strong feelings about this, I just need to know one way or the other from People Who Know(TM) because I have a book manuscript to turn in and I'd like to get it mostly right. :)

No Comments