为什么我的SqlDataSource的UpdateCommand会工作吗? [英] Why isn't my SqlDataSource's UpdateCommand working?

查看:148
本文介绍了为什么我的SqlDataSource的UpdateCommand会工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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为

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

WHERE [ID] = @id

WHERE [id] = @id

WHERE [regGroupID] = 2

WHERE [regGroupID] = 2

推荐答案

您需要添加reg​​GroupID到的DataKeyNames 收集你的的GridView 声明。事情是这样的:

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

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

查看<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx\"><$c$c>DataKeyNames文档:

您必须设置DataKeyNames属性,以便自动
  GridView控件的更新和删除功能的工作。价值
  这些键字段被传递给该数据源控制,以
  指定的行更新或删除。

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"

这篇关于为什么我的SqlDataSource的UpdateCommand会工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆