繁体   English   中英

SqlDataSource UpdateCommand 不工作

[英]SqlDataSource UpdateCommand is not working

我有一个GridView ,它的 DataSource 是一个SqlDataSource

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    UpdateCommand="UPDATE chemlab_Registerkarte SET [Name] =@Name WHERE [regID] =@regID;" OnUpdating="Update"
    SelectCommandType="Text" SelectCommand='select [regID], [Name] from chemlab_Registerkarte;'>
</asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="regID, Name">
    <Columns>
        <asp:CommandField ShowEditButton="True" CancelText="<%$ Resources:LocalizedText,TextCancel %>"
            EditText="<%$ Resources:LocalizedText,TextEdit %>" UpdateText="<%$ Resources:LocalizedText,TextSave %>"/>
        <asp:BoundField DataField="regID" HeaderText="regID" ReadOnly="True" SortExpression="regID"
            Visible="true" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"/>
    </Columns>
</asp:GridView>

我的问题是,它没有更新,尽管 select 命令工作正常。

我看过这个问题: Why isn't my SqlDataSource's UpdateCommand working? 但是我包含了DataKeyNames (如您所见),所以这不是我的错误。

我做错了什么?

编辑

我尝试使用以下代码从我的 GridView 中获取数据:

String[] values = new String[2];    
values[0] = this.GridView1.Rows[0].Cells[0].Text;
values[1] = this.GridView1.Rows[0].Cells[1].Text;

当我不编辑它时我得到值,如果我这样做我得到一个空字符串。 因此我什至不能手动更新我的数据库。

编辑 2

我有另一个工作的网站:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
        UpdateCommand="UPDATE chemlab_Anlagen SET Kuerzel =@Kuerzel, Fuellvolumen =@Fuellvolumen, aktiv =@aktiv where aID=@aID;"
        SelectCommandType="Text" SelectCommand='select a.aID, a.InventarNum, m.MO_name, a.Kuerzel, a.Fuellvolumen, a.aktiv from chemlab_Anlagen as a join vw_chemlab_MaintenanceObject as m on a.InventarNum = m.MO_key;'>
    </asp:SqlDataSource>

 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="InventarNum,aID,MO_name">
            <Columns>
                <asp:CommandField ShowEditButton="True" CancelText="<%$ Resources:LocalizedText,TextCancel %>"
                    EditText="<%$ Resources:LocalizedText,TextEdit %>" UpdateText="<%$ Resources:LocalizedText,TextSave %>" />
                <asp:BoundField DataField="aID" HeaderText="aID" ReadOnly="True" SortExpression="aID"
                    Visible="false" />
                <asp:BoundField DataField="InventarNum" HeaderText="InventarNum" ReadOnly="True"
                    SortExpression="InventarNum" />
                <asp:BoundField DataField="MO_name" HeaderText="MO_name" SortExpression="MO_name"
                    ReadOnly="true" />
                <asp:BoundField DataField="Kuerzel" HeaderText="Kuerzel" SortExpression="Kuerzel" />
                <asp:BoundField DataField="Fuellvolumen" HeaderText="Fuellvolumen" SortExpression="Fuellvolumen" />
                <asp:CheckBoxField DataField="aktiv" HeaderText="aktiv" SortExpression="aktiv" />
            </Columns>
        </asp:GridView>

我自己找到了解决方案,这是一个非常愚蠢的错误。

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="regID, Name">

字段DataKeyNames仅适用于主键。 我刚刚从此属性中删除了“名称”(因为它不是主键)并且可以正常工作。

https://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.gridview.datakeynames(v=vs.110).aspx

我也有这个问题,结果是我将以下设置为 ="False"

EnableSortingAndPagingCallbacks="True"

<asp:GridView ID="My_GridView" runat="server EnableSortingAndPagingCallbacks="True">....</asp:GridView>

当我将其更改为 true(或删除它)时,我的更新按钮起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM