2

从 GridView 更新表时,我遇到了数据损坏的小问题。我必须承认这是我第一次尝试 asp.net,但我对 C# 相当熟悉。

首先,一些背景。我们有一些第三方库存/订单处理软件,将其数据存储在 MSSQL 中。我正在使用 GridView 和 sql 查询来提取当前工作订单并将它们显示在表格上。sql 查询还结合了我创建的数据库 (tblProdStatus) 中的另一个表,该表添加了有关工作订单的其他状态信息。

网页允许编辑,以便可以更新工单的状态,并且这些更改需要写回表 (tblProdStatus)。asp.GridView 具有使用适当参数定义的UpdateCommand 。我还定义了DataKeyNames来标识正在更新的行。

一切都按预期工作,我可以编辑一行、更新字段、选择更新并将信息正确写入表 (tblProdStatus)。

我的问题是在编辑过程中在 3rd 方软件中更改了基础数据。数据最终与错误的记录相关联,根据第三个软件是否添加或删除了一行,向上或向下移动一行。

第 3 方软件有一个 AllocID 密钥,该密钥与一个工作订单相关联,并且在持续时间内不会更改。我将此链接到存储在我的表 (tblProdStatus) 中的相同数字,以将两个表联系在一起。当损坏发生时,sql 更新传递了错误的 AllocID 用于正在编辑的行。这几乎就像是在单击更新链接时刷新了表,然后使用错误的行提交了数据。

逻辑背后没有真正的代码,它都在 aspx 文件中。我已经发布了,希望不会让我自己太尴尬。

我经常使用 stackoverflow,并且似乎总是能够在我的问题中找到其他人的指导,所以这是我第一篇关于实际问题的帖子。非常感谢您的任何建议。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="prodStatus.aspx.cs" Inherits="ProdStatus.prodStatus" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvProdStatus" runat="server" AutoGenerateColumns="False" DataKeyNames="AllocID"
            DataSourceID="SqlDS" onrowdatabound="gvProdStatus_RowDataBound" 
            CssClass="table" EnableViewState="False" >
            <Columns>
                <asp:BoundField DataField="AllocID" HeaderText="AllocID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="AllocID" Visible="false" />
                <asp:BoundField DataField="AllocWONo" HeaderText="Works Order"
                    ReadOnly="True" SortExpression="Works Order" />
                <asp:BoundField DataField="ShortNm" HeaderText="Cust" 
                    ReadOnly="True" SortExpression="Cust" />
                <asp:BoundField DataField="CustPONo" HeaderText="Cust PO" 
                    ReadOnly="True" SortExpression="Cust PO" />
                <asp:BoundField DataField="MasterPNo" HeaderText="Part Number" 
                    ReadOnly="True" SortExpression="Part Number" />
                <asp:BoundField DataField="ItemDescription" HeaderText="Description" 
                    ReadOnly="True" SortExpression="Description" />
                <asp:BoundField DataField="AllocQty" HeaderText="Qty" 
                    ReadOnly="True" SortExpression="Qty" DataFormatString="{0:G29}" />
                <asp:BoundField DataField="ReqdDate" HeaderText="Due" 
                    ReadOnly="True" SortExpression="Due" DataFormatString="{0:d/MMM/yy}" />

                <asp:TemplateField HeaderText="Done" SortExpression="state0">
                    <ItemTemplate>
                        <asp:CheckBox ID="state0" runat="server" Enabled="false"
                        Checked='<%#Eval("state0") %>'
                        Visible='<%#Eval("mask0") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state0" runat="server"
                        Checked='<%#Bind("state0") %>'
                        Visible='<%#Eval("mask0") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/lfp.jpg' alt='Laser Faceplate' />" SortExpression="state1">
                    <ItemTemplate>
                        <asp:CheckBox ID="state1" runat="server" Enabled="false"
                        Checked='<%#Eval("state1") %>'
                        Visible='<%#Eval("mask1") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state1" runat="server"
                        Checked='<%#Bind("state1") %>'
                        Visible='<%#Eval("mask1") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/lcv.jpg' alt='Laser Cover' />" SortExpression="state2">
                    <ItemTemplate>
                        <asp:CheckBox ID="state2" runat="server" Enabled="false"
                        Checked='<%#Eval("state2") %>'
                        Visible='<%#Eval("mask2") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state2" runat="server"
                        Checked='<%#Bind("state2") %>'
                        Visible='<%#Eval("mask2") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/hdw.jpg' alt='Hardware Pack' />" SortExpression="state3">
                    <ItemTemplate>
                        <asp:CheckBox ID="state3" runat="server" Enabled="false"
                        Checked='<%#Eval("state3") %>'
                        Visible='<%#Eval("mask3") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state3" runat="server"
                        Checked='<%#Bind("state3") %>'
                        Visible='<%#Eval("mask3") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/str.jpg' alt='Strike Plate' />" SortExpression="state4">
                    <ItemTemplate>
                        <asp:CheckBox ID="state4" runat="server" Enabled="false"
                        Checked='<%#Eval("state4") %>'
                        Visible='<%#Eval("mask4") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state4" runat="server"
                        Checked='<%#Bind("state4") %>'
                        Visible='<%#Eval("mask4") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/box.jpg' alt='Box' />" SortExpression="state5">
                    <ItemTemplate>
                        <asp:CheckBox ID="state5" runat="server" Enabled="false"
                        Checked='<%#Eval("state5") %>'
                        Visible='<%#Eval("mask5") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state5" runat="server"
                        Checked='<%#Bind("state5") %>'
                        Visible='<%#Eval("mask5") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/ins.jpg' alt='Instructions' />" SortExpression="state6">
                    <ItemTemplate>
                        <asp:CheckBox ID="state6" runat="server" Enabled="false"
                        Checked='<%#Eval("state6") %>'
                        Visible='<%#Eval("mask6") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state6" runat="server"
                        Checked='<%#Bind("state6") %>'
                        Visible='<%#Eval("mask6") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="<img src='./images/pcb.jpg' alt='PCBs' />" SortExpression="state7">
                    <ItemTemplate>
                        <asp:CheckBox ID="state7" runat="server" Enabled="false"
                        Checked='<%#Eval("state7") %>'
                        Visible='<%#Eval("mask7") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="state7" runat="server"
                        Checked='<%#Bind("state7") %>'
                        Visible='<%#Eval("mask7") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Note" SortExpression="Note">
                    <ItemTemplate>
                        <asp:Label ID="notes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="notes" runat="server" Text='<%# Bind("notes") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:CommandField ShowEditButton="True" />

            </Columns>
        </asp:GridView>

        <asp:SqlDataSource ID="SqlDS" runat="server" 
            ConnectionString="<%$ ConnectionStrings:worksOrdersConnectionString %>" 

            SelectCommand = "SELECT [AllocID], [tblCustOrders].[CustORID], [tblStockItems].[ItemID], [ReqdDate], [AllocWONo], [ShortNm], [tblAlloc].[AllocQty], [tblCustOrders].[CustPONo], [MasterPNo], [ItemDescription], 

                ISNULL([mask0], 0) as [mask0],
                ISNULL([mask1], 0) as [mask1],
                ISNULL([mask2], 0) as [mask2],
                ISNULL([mask3], 0) as [mask3],
                ISNULL([mask4], 0) as [mask4],
                ISNULL([mask5], 0) as [mask5],
                ISNULL([mask6], 0) as [mask6],
                ISNULL([mask7], 0) as [mask7],

                ISNULL([state0], 0) as [state0],
                ISNULL([state1], 0) as [state1],
                ISNULL([state2], 0) as [state2],
                ISNULL([state3], 0) as [state3],
                ISNULL([state4], 0) as [state4],
                ISNULL([state5], 0) as [state5],
                ISNULL([state6], 0) as [state6],
                ISNULL([state7], 0) as [state7],
                ISNULL([notes], '') as [notes]

                FROM (((((([miniMrpDB].[dbo].[tblAlloc]
                INNER JOIN [miniMrpDB].[dbo].[tblSalesOrderDetail] ON [tblSalesOrderDetail].[RowID] = [tblAlloc].[SalesRowID])
                INNER JOIN [miniMrpDB].[dbo].[tblStockItems] ON [tblStockItems].[ItemID] = [tblSalesOrderDetail].[StockID])
                INNER JOIN [miniMrpDB].[dbo].[tblCustOrders] ON [tblCustOrders].[CustORID] = [tblAlloc].[CustORID])
                INNER JOIN [miniMrpDB].[dbo].[tblCusAddresses] ON [tblCustOrders].[CustID] = [tblCusAddresses].[AddID])
                LEFT JOIN [worksOrders].[dbo].[tblItemMaskBits] ON [tblStockItems].[ItemID] = [tblItemMaskBits].[ItemID])
                LEFT JOIN [worksOrders].[dbo].[tblProdStatus] ON [AllocID] = [tblAllocID])
                WHERE (Status IS NULL OR Status < 3) AND ([tblAlloc].[CustORID] > -1) AND ([AllocID] < 999999999) ORDER BY [ReqdDate], [ShortNm], [CustPONo], [AllocWONo]"

            UpdateCommand = "BEGIN TRAN
               IF EXISTS (SELECT * from [worksOrders].[dbo].[tblProdStatus] WITH (updlock,serializable) WHERE [tblAllocID] = (@AllocID))
                  BEGIN
                   UPDATE [worksOrders].[dbo].[tblProdStatus] SET
                      [state0] = (@State0),
                      [state1] = (@State1),
                      [state2] = (@State2),
                      [state3] = (@State3),
                      [state4] = (@State4),
                      [state5] = (@State5),
                      [state6] = (@State6),
                      [state7] = (@State7),
                      [notes] = (@Notes)
                      WHERE [tblAllocID] = (@AllocID)
                  END
               ELSE
                  BEGIN
                      INSERT [worksOrders].[dbo].[tblProdStatus] ([tblAllocID], [state0], [state1], [state2], [state3], [state4], [state5], [state6], [state7], [notes])
                      VALUES ((@AllocID), (@State0), (@State1), (@State2), (@State3), (@State4), (@State5), (@State6), (@State7), (@Notes))
                    END
               COMMIT TRAN" >

            <UpdateParameters>
                <asp:Parameter Type="Int32" Name="AllocID"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State0"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State1"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State2"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State3"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State4"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State5"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State6"></asp:Parameter>
                <asp:Parameter Type="Boolean" Name="State7"></asp:Parameter>
                <asp:Parameter Type="String" Name="Notes"></asp:Parameter>
            </UpdateParameters>

        </asp:SqlDataSource>
    </div>
    <br />
    <div>
    <asp:Button ID="btnReload" runat="server" Text="Reload" 
        onclick="btnReload_Click" />
    <a id="countdown"></p>
    </div>

    <script>
    <!--
        (function countdown(remaining) {
            if (remaining === 0)
                location.reload(true);
            document.getElementById('countdown').innerHTML = remaining;
            setTimeout(function () { countdown(remaining - 1); }, 1000);
        })(600);
    //-->
    </script>

    </form>
</body>
</html>
4

0 回答 0