GridView生成INSERT,UPDATE和DELETE语句

时间:2018-03-01 19:39:36

标签: c# asp.net sql-server

我有一个带按钮的非常简单的搜索框。这将回发到带有网格视图的结果页面。查询工作正常,但是当我进入高级SQL生成选项并选择“生成INSERT,UPDATE和DELETE语句”,然后在GridView任务中选中“启用编辑”时,它不起作用。

我使用搜索框,查询返回结果,我点击要编辑的行上的编辑按钮,页面变为空白。这是特别令人困惑的,因为这是我在其他使用gridviews的页面上所做的一切,并且它没有问题。

我试图通过在线研究问题来解决这个问题,但我不知道该怎么做。如果绑定ID列,我觉得我正指向方向,但我不确定,我不熟悉绑定; ID列是表的键列。非常感谢任何帮助。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="resultssm.aspx.cs" Inherits="search_shipping_resultssm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server"
            AutoGenerateColumns="False" 
            DataKeyNames="ID" 
            DataSourceID="SqlDataSource1" 
            EnableModelValidation="True">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="ItemNumber" HeaderText="ItemNumber" SortExpression="ItemNumber" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
                <asp:BoundField DataField="RoomName" HeaderText="RoomName" SortExpression="RoomName" />
                <asp:BoundField DataField="WorkOrderName" HeaderText="WorkOrderName" SortExpression="WorkOrderName" />
                <asp:BoundField DataField="Height" HeaderText="Height" SortExpression="Height" />
                <asp:BoundField DataField="Width" HeaderText="Width" SortExpression="Width" />
                <asp:BoundField DataField="Depth" HeaderText="Depth" SortExpression="Depth" />
                <asp:BoundField DataField="ReleaseDate" HeaderText="ReleaseDate" SortExpression="ReleaseDate" />
                <asp:BoundField DataField="ProductionStartDate" HeaderText="ProductionStartDate" SortExpression="ProductionStartDate" />
                <asp:BoundField DataField="StageDate" HeaderText="StageDate" SortExpression="StageDate" />
                <asp:BoundField DataField="ShipDate" HeaderText="ShipDate" SortExpression="ShipDate" />
                <asp:BoundField DataField="DeliveryDate" HeaderText="DeliveryDate" SortExpression="DeliveryDate" />
                <asp:BoundField DataField="InstallDate" HeaderText="InstallDate" SortExpression="InstallDate" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:IQConnectionString %>"
            SelectCommand="SELECT * FROM [ProductTracking] WHERE ([WorkOrderName] = @WorkOrderName)" 
            DeleteCommand="DELETE FROM [ProductTracking] WHERE [ID] = @ID"
            InsertCommand="INSERT INTO [ProductTracking] ([ID], [ItemNumber], [Name], [Quantity], [RoomName], [WorkOrderName], [Height], [Width], [Depth], [ReleaseDate], [ProductionStartDate], [StageDate], [ShipDate], [DeliveryDate], [InstallDate]) VALUES (@ID, @ItemNumber, @Name, @Quantity, @RoomName, @WorkOrderName, @Height, @Width, @Depth, @ReleaseDate, @ProductionStartDate, @StageDate, @ShipDate, @DeliveryDate, @InstallDate)"
            UpdateCommand="UPDATE [ProductTracking] SET [ItemNumber] = @ItemNumber, [Name] = @Name, [Quantity] = @Quantity, [RoomName] = @RoomName, [WorkOrderName] = @WorkOrderName, [Height] = @Height, [Width] = @Width, [Depth] = @Depth, [ReleaseDate] = @ReleaseDate, [ProductionStartDate] = @ProductionStartDate, [StageDate] = @StageDate, [ShipDate] = @ShipDate, [DeliveryDate] = @DeliveryDate, [InstallDate] = @InstallDate WHERE [ID] = @ID">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="ID" Type="String" />
                <asp:Parameter Name="ItemNumber" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Quantity" Type="Int32" />
                <asp:Parameter Name="RoomName" Type="String" />
                <asp:Parameter Name="WorkOrderName" Type="String" />
                <asp:Parameter Name="Height" Type="Double" />
                <asp:Parameter Name="Width" Type="Double" />
                <asp:Parameter Name="Depth" Type="Double" />
                <asp:Parameter DbType="DateTime2" Name="ReleaseDate" />
                <asp:Parameter DbType="DateTime2" Name="ProductionStartDate" />
                <asp:Parameter DbType="DateTime2" Name="StageDate" />
                <asp:Parameter DbType="DateTime2" Name="ShipDate" />
                <asp:Parameter DbType="DateTime2" Name="DeliveryDate" />
                <asp:Parameter DbType="DateTime2" Name="InstallDate" />
            </InsertParameters>
            <SelectParameters>
                <asp:FormParameter 
                    FormField="SMSearchBox" 
                    Name="WorkOrderName" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="ItemNumber" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Quantity" Type="Int32" />
                <asp:Parameter Name="RoomName" Type="String" />
                <asp:Parameter Name="WorkOrderName" Type="String" />
                <asp:Parameter Name="Height" Type="Double" />
                <asp:Parameter Name="Width" Type="Double" />
                <asp:Parameter Name="Depth" Type="Double" />
                <asp:Parameter DbType="DateTime2" Name="ReleaseDate" />
                <asp:Parameter DbType="DateTime2" Name="ProductionStartDate" />
                <asp:Parameter DbType="DateTime2" Name="StageDate" />
                <asp:Parameter DbType="DateTime2" Name="ShipDate" />
                <asp:Parameter DbType="DateTime2" Name="DeliveryDate" />
                <asp:Parameter DbType="DateTime2" Name="InstallDate" />
                <asp:Parameter Name="ID" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>

0 个答案:

没有答案