从DataGridView删除SQL没有发生

时间:2016-11-16 10:31:08

标签: c# sql asp.net gridview sqldatasource

我有一个已启用删除功能的DataGridView,但它从未发生过。整个网格消失(多行),但我点击的行的删除永远不会发生。

我可以在sql management studio中手动运行sql语句,它运行得很好,但似乎从来没有从网页运行。

我不知道从哪里开始,我之前没有遇到像GridView这样的问题。

的SqlDataSource:

<asp:SqlDataSource ID="SqlDataSourceVisitors" runat="server" ConnectionString="constring" DeleteCommand="DELETE FROM [RegisteredVisitors] WHERE [PK] = @PK UPDATE [dbo].[Users] SET [NumberOfVisitors] = (SELECT [NumberOfVisitors] FROM [dbo].[NumberOfVisitors] WHERE [ResidentName] = @ResidentName) -1 WHERE [ResidentName] = @ResidentName" InsertCommand="INSERT INTO [RegisteredVisitors] ([ResidentName], [ResidentAddress], [VisitorCode], [VisitorName]) VALUES (@ResidentName, @ResidentAddress, @VisitorCode, @VisitorName)" SelectCommand="SELECT * FROM [RegisteredVisitors] WHERE ([ResidentName] = @ResidentName)" UpdateCommand="UPDATE [RegisteredVisitors] SET [ResidentName] = @ResidentName, [ResidentAddress] = @ResidentAddress, [VisitorCode] = @VisitorCode, [VisitorName] = @VisitorName WHERE [PK] = @PK" ProviderName="System.Data.SqlClient">
        <DeleteParameters>
            <asp:Parameter Name="PK" Type="Int32" />
            <asp:Parameter Name="ResidentName" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ResidentName" Type="String" />
            <asp:Parameter Name="ResidentAddress" Type="String" />
            <asp:Parameter Name="VisitorCode" Type="Int32" />
            <asp:Parameter Name="VisitorName" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="txtboxSelectedResident" Name="ResidentName" PropertyName="Text" Type="String" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ResidentName" Type="String" />
            <asp:Parameter Name="ResidentAddress" Type="String" />
            <asp:Parameter Name="VisitorCode" Type="Int32" />
            <asp:Parameter Name="VisitorName" Type="String" />
            <asp:Parameter Name="PK" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

GridView的:

<asp:GridView ID="DataGridViewVisitors" runat="server" CellPadding="4" DataSourceID="SqlDataSourceVisitors" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:1)

如果我是对的,你只是错过了DeleteCommand中的; 。写入命令内的所有内容都将在一次执行中执行。因此,要分离出您需要的删除和更新查询;。

DeleteCommand="DELETE FROM [RegisteredVisitors] WHERE [PK] = @PK; UPDATE [dbo].[Users] SET [NumberOfVisitors] = (SELECT [NumberOfVisitors] FROM [dbo].[NumberOfVisitors] WHERE [ResidentName] = @ResidentName) -1 WHERE [ResidentName] = @ResidentName"