加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 为什么我的SqlDataSource的UpdateCommand不工作?

发布时间:2020-12-16 06:26:20 所属栏目:asp.Net 来源:网络整理
导读:asp:SqlDataSource ID="HopefulDataSource" runat="server" ConnectionString="%$ConnectionStrings:MyConnectionString %" SelectCommand= "SELECT id,regGroupID,amountReceived,other FROM table" UpdateCommand="UPDATE table SET [amountReceived] = @a
<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 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.

注意:自从我使用它以来已经有一段时间了,因此您可能需要同时包含主键和其他键字段.像这样:

DataKeyNames="id,regGroupID"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读