[英]Why isn't my SqlDataSource's UpdateCommand working?
<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>
当我将“WHERE [regGroupID] = @regGroupID”更改为任何一个时,上述工作
WHERE [id] = @id
要么
WHERE [regGroupID] = 2
您需要在GridView
声明中将“regGroupID”添加到DataKeyNames
集合中。 像这样的东西:
<asp:GridView ID="yourGridViewId" DataKeyNames="regGroupID" ... >
...
</asp:GridView>
请参阅DataKeyNames
文档 :
必须设置DataKeyNames属性才能使GridView控件的自动更新和删除功能起作用。 这些键字段的值将传递到数据源控件,以指定要更新或删除的行。
注意:自从我使用它以来已经有一段时间了,因此您可能需要同时包含主键和其他键字段。 像这样:
DataKeyNames="id,regGroupID"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.