ASP.NET listview引用完整性

时间:2014-09-25 05:42:50

标签: c# asp.net sql-server

我在ASP.NET,C#,SQL Server 2014 Express中设置了Listview。我还在我的数据库中设置了许多外键。当用户试图删除某些内容时,我想告诉他们“你不能删除这条记录,因为它存在于另一个表中”

当没有参照完整性冲突时,一切正常。

KitID将是此硬件表中其中一个外键的一个很好的示例。

非常感谢任何帮助。

非常新的ASP.NET,所以请不要在回复时承担太多的知识。谢谢! :)

以下代码

<div>

    <asp:SqlDataSource ID="SqlDataSourceHardware" runat="server" OnUpdating="SqlDataSourceHardware_Updating" EnableCaching="False"
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        DeleteCommand="DELETE FROM [Hardware] WHERE [HardwareID] = @HardwareID" 
        InsertCommand="INSERT INTO [Hardware] ([HardwareName], [NSN], [AssetNo], [SerialNo], [ModelNo], [LastStockCheck], [StockCheckFreqDays], [DisposedOfDate], [DisposedBy], [WarrantyExpDate], [SustainedBy], [OwnedBy], [IsVirtualMachine], [NonTechFreqDays], [LastNonTechDate], [NonTechInstructions], [SignedOutBy], [Location], [LoanedTo], [LoanedToLocation], [ExpectedLoanReturnDate], [ServiceabilityID], [KitID], [ImagePath], [ImagePathThumb]) VALUES (@HardwareName, @NSN, @AssetNo, @SerialNo, @ModelNo, @LastStockCheck, @StockCheckFreqDays, @DisposedOfDate, @DisposedBy, @WarrantyExpDate, @SustainedBy, @OwnedBy, @IsVirtualMachine, @NonTechFreqDays, @LastNonTechDate, @NonTechInstructions, @SignedOutBy, @Location, @LoanedTo, @LoanedToLocation, @ExpectedLoanReturnDate, @ServiceabilityID, @KitID, @ImagePath, @ImagePathThumb)" 

        UpdateCommand="UPDATE [Hardware] SET [HardwareName] = @HardwareName, [NSN] = @NSN, [AssetNo] = @AssetNo, [SerialNo] = @SerialNo, [ModelNo] = @ModelNo, [LastStockCheck] = @LastStockCheck, [StockCheckFreqDays] = @StockCheckFreqDays, [DisposedOfDate] = @DisposedOfDate, [DisposedBy] = @DisposedBy, [WarrantyExpDate] = @WarrantyExpDate, [SustainedBy] = @SustainedBy, [OwnedBy] = @OwnedBy, [IsVirtualMachine] = @IsVirtualMachine, [NonTechFreqDays] = @NonTechFreqDays, [LastNonTechDate] = @LastNonTechDate, [NonTechInstructions] = @NonTechInstructions, [SignedOutBy] = @SignedOutBy, [Location] = @Location, [LoanedTo] = @LoanedTo, [LoanedToLocation] = @LoanedToLocation, [ExpectedLoanReturnDate] = @ExpectedLoanReturnDate, [ImagePath] = @ImagePath, [ImagePathThumb] = @ImagePathThumb, [ServiceabilityID] = @ServiceabilityID, [KitID] = @KitID WHERE [HardwareID] = @HardwareID">
        <DeleteParameters>
            <asp:Parameter Name="HardwareID" Type="Int32" />
        </DeleteParameters>

        <InsertParameters>
            <asp:Parameter Name="HardwareName" Type="String" />
            <asp:Parameter Name="NSN" Type="String" />
            <asp:Parameter Name="AssetNo" Type="String" />
            <asp:Parameter Name="SerialNo" Type="String" />
            <asp:Parameter Name="ModelNo" Type="String" />
            <asp:Parameter Name="LastStockCheck" Type="String" />
            <asp:Parameter Name="StockCheckFreqDays" Type="Int32" />
            <asp:Parameter DbType="Date" Name="DisposedOfDate" />
            <asp:Parameter Name="DisposedBy" Type="String" />
            <asp:Parameter DbType="Date" Name="WarrantyExpDate" />
            <asp:Parameter Name="SustainedBy" Type="String" />
            <asp:Parameter Name="OwnedBy" Type="String" />
            <asp:Parameter Name="IsVirtualMachine" Type="Boolean" />
            <asp:Parameter Name="NonTechFreqDays" Type="Int32" />
            <asp:Parameter DbType="Date" Name="LastNonTechDate" />
            <asp:Parameter Name="NonTechInstructions" Type="String" />
            <asp:Parameter Name="SignedOutBy" Type="String" />
            <asp:Parameter Name="Location" Type="String" />
            <asp:Parameter Name="LoanedTo" Type="String" />
            <asp:Parameter Name="LoanedToLocation" Type="String" />
            <asp:Parameter DbType="Date" Name="ExpectedLoanReturnDate" />
            <asp:Parameter Name="ServiceabilityID" Type="Int32" />
            <asp:Parameter Name="KitID" Type="Int32" />
            <asp:Parameter Name="ImagePath" Type="String" />
            <asp:Parameter Name="ImagePathThumb" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="1" Name="HardwareID" 
                QueryStringField="HardwareID" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="HardwareName" Type="String" />
            <asp:Parameter Name="NSN" Type="String" />
            <asp:Parameter Name="AssetNo" Type="String" />
            <asp:Parameter Name="SerialNo" Type="String" />
            <asp:Parameter Name="ModelNo" Type="String" />
            <asp:Parameter Name="LastStockCheck" Type="String" />
            <asp:Parameter Name="StockCheckFreqDays" Type="Int32" />
            <asp:Parameter DbType="Date" Name="DisposedOfDate" />
            <asp:Parameter Name="DisposedBy" Type="String" />
            <asp:Parameter DbType="Date" Name="WarrantyExpDate" />
            <asp:Parameter Name="SustainedBy" Type="String" />
            <asp:Parameter Name="OwnedBy" Type="String" />
            <asp:Parameter Name="IsVirtualMachine" Type="Boolean" />
            <asp:Parameter Name="NonTechFreqDays" Type="Int32" />
            <asp:Parameter DbType="Date" Name="LastNonTechDate" />
            <asp:Parameter Name="NonTechInstructions" Type="String" />
            <asp:Parameter Name="SignedOutBy" Type="String" />
            <asp:Parameter Name="Location" Type="String" />
            <asp:Parameter Name="LoanedTo" Type="String" />
            <asp:Parameter Name="LoanedToLocation" Type="String" />
            <asp:Parameter DbType="Date" Name="ExpectedLoanReturnDate" />
            <asp:Parameter Name="ServiceabilityID" Type="Int32" />
            <asp:Parameter Name="KitID" Type="Int32" />
            <asp:Parameter Name="HardwareID" Type="Int32" />
            <asp:Parameter Name="ImagePath" Type="String" />
            <asp:Parameter Name="ImagePathThumb" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="HardwareID" OnItemDeleted="FormView1_OnDeletedItem" 
        DataSourceID="SqlDataSourceHardware" 
        EmptyDataText="No Information Available<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />">
        <EditItemTemplate>

            <table id="HardwareItem" class="table table-bordered span12">
                <thead>
                    <tr>
                        <th colspan="2" class="span12">Details</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="span6">GeoQ ID:</td>
                        <td class="span6">
                            <asp:Label ID="HardwareIDLabel" runat="server" 
                                Text='<%# Eval("HardwareID") %>' />                            
                        </td>
                    </tr>
                    <tr>
                        <td>Hardware Name:</td>
                        <td>
                            <asp:RequiredFieldValidator ID="HardwareNameValidator" runat="server" ErrorMessage="Hardware Name Required" ControlToValidate="HardwareNameTextBox" CssClass="ValidationError" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
                            <asp:TextBox ID="HardwareNameTextBox" runat="server" maxlength="100" 
                            Text='<%# Bind("HardwareName") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>NSN:</td>
                        <td>
                            <asp:TextBox ID="NSNTextBox" runat="server" maxlength="50" 
                            Text='<%# Bind("NSN") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Asset No:</td>
                        <td>
                            <asp:TextBox ID="AssetNoTextBox" runat="server" maxlength="50" 
                            Text='<%# Bind("AssetNo") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Serial No:</td>
                        <td>
                            <asp:RequiredFieldValidator ID="SerialNoValidator" runat="server" ErrorMessage="Serial No Required" ControlToValidate="SerialNoTextBox" CssClass="ValidationError" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
                            <asp:TextBox ID="SerialNoTextBox" runat="server" maxlength="50" 
                            Text='<%# Bind("SerialNo") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Model No:</td>
                        <td>
                            <asp:RequiredFieldValidator ID="ModelNoValidator" runat="server" ErrorMessage="Model No Required" ControlToValidate="ModelNoTextBox" CssClass="ValidationError" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
                            <asp:TextBox ID="ModelNoTextBox" runat="server" maxlength="50" 
                            Text='<%# Bind("ModelNo") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Is This a Virtual Machine:</td>
                        <td>
                            <asp:CheckBox ID="IsVirtualMachineCheckBox" runat="server" Checked='<%# Bind("IsVirtualMachine") %>' />
                        </td>
                    </tr>

                    <tr>
                        <td>Serviceability:</td>
                        <td>               
                            <asp:RequiredFieldValidator ID="ServiceabilityValidator" runat="server" ErrorMessage="Serviceability Required" ControlToValidate="DropDownListServiceability" CssClass="ValidationError" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
                            <asp:DropDownList ID="DropDownListServiceability" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="ServiceabilityList" DataTextField="ServiceabilityName" DataValueField="ServiceabilityID"  
                                SelectedValue='<%# Bind("ServiceabilityID") %>'>
                                <asp:listitem value="" text="Select a value"></asp:listitem>
                            </asp:DropDownList>

                            <asp:SqlDataSource ID="ServiceabilityList" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
                                SelectCommand="SELECT * FROM [Serviceability]">
                            </asp:SqlDataSource>  

                        </td>
                    </tr>
                    <tr>
                        <td>Kit Name:</td>
                        <td>
                            <asp:DropDownList ID="DropDownList_Kit" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="KitList" DataTextField="KitName" DataValueField="KitID"  
                                SelectedValue='<%# Bind("KitID") %>'>
                                <asp:listitem value="" text="Select a kit"></asp:listitem>
                            </asp:DropDownList>

                            <asp:SqlDataSource ID="KitList" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
                                SelectCommand="SELECT * FROM [Kit]">
                            </asp:SqlDataSource>                         
                        </td>
                    </tr>

                    <tr>
                        <td>Image:</td>
                        <td>
                            <asp:FileUpload ID="ImageFileUpload" AllowMultiple="false" ClientIDMode="Static" runat="server" />
                            <asp:HiddenField ID="ImagePathHdn" ClientIDMode="Static" runat="server" Value='<%# Bind("ImagePath") %>' /> 
                            <asp:HiddenField ID="ImagePathThumbHdn" ClientIDMode="Static" runat="server" Value='<%# Bind("ImagePathThumb") %>' /> 
                            <asp:HiddenField ID="DBImagePath" ClientIDMode="Static" runat="server" Value='<%# Bind("fullPath") %>' /> 
                            <asp:HiddenField ID="DBImagePathThumb" ClientIDMode="Static" runat="server" Value='<%# Bind("fullPathThumb") %>' /> 
                            <div id="uploadPreview"><a rel="prettyPhoto" title="<%# Eval("HardwareName") %>" href="<%# Request.ApplicationPath + Eval("ImagePath")%>"><img ID="imgPreview" src="<%# Request.ApplicationPath + Eval("ImagePathThumb") + "?" + DateTime.Now.Ticks.ToString() %>" class="imgPreview img-rounded" onError="this.src='images/NoImage.png';" /></a></div>

                        </td>
                    </tr>

                </tbody>
            </table>
          </div>


          <div class="nopad">
            <table id="Governance" class="table table-bordered span12">
                <thead>
                    <tr>
                        <th colspan="2" class="span12">Governance</th>
                    </tr>
                </thead>
                <tbody>


                    <tr>
                        <td class="span6">Last Stock Check Date:</td>
                        <td class="span6">
                            <asp:TextBox ID="LastStockCheck_aspnetdatepicker" runat="server" 
                            Text='<%# Bind("LastStockCheck", "{0:dd MMM yyyy}") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Stock Check Frequency (Days):</td>
                        <td>
                            <asp:RangeValidator ID="StockCheckFreqDaysValidator" runat="server" ErrorMessage="Range must be between 1 and 1000" MaximumValue="1000" MinimumValue="1" SetFocusOnError="True" CssClass="ValidationError" ControlToValidate="StockCheckFreqDaysTextBox" Type="Integer"></asp:RangeValidator>
                            <asp:TextBox ID="StockCheckFreqDaysTextBox" runat="server" 
                            Text='<%# Bind("StockCheckFreqDays") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Disposed Of Date:</td>
                        <td>
                            <asp:TextBox ID="DisposedOfDate_aspnetdatepicker" runat="server" 
                            Text='<%# Bind("DisposedOfDate", "{0:dd MMM yyyy}") %>' />                        
                        </td>
                    </tr>
                    <tr>
                        <td>Disposed By:</td>
                        <td>

                            <asp:DropDownList ID="DropDownList_DisposedBy" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="ProfileList" DataTextField="UserName" DataValueField="UserId"  
                                SelectedValue='<%# Bind("DisposedBy") %>'>
                                <asp:listitem value="" text="Select a person"></asp:listitem>
                            </asp:DropDownList>


                        </td>
                    </tr>
                    <tr>
                        <td>Warranty Expiry Date:</td>
                        <td>
                            <asp:TextBox ID="WarrantyExpDate_aspnetdatepicker" runat="server" 
                            Text='<%# Bind("WarrantyExpDate", "{0:dd MMM yyyy}") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Non-Tech Frequency (Days):</td>
                        <td>
                            <asp:RangeValidator ID="NonTechFreqDaysTextBoxValidator" runat="server" ErrorMessage="Range must be between 1 and 1000" MaximumValue="1000" MinimumValue="1" SetFocusOnError="True" CssClass="ValidationError" ControlToValidate="NonTechFreqDaysTextBox" Type="Integer"></asp:RangeValidator>
                            <asp:TextBox ID="NonTechFreqDaysTextBox" runat="server" 
                            Text='<%# Bind("NonTechFreqDays") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Last Non-Tech Date:</td>
                        <td>


                            <asp:TextBox ID="LastNonTechDate_aspnetdatepicker" runat="server" 
                            Text='<%# Bind("LastNonTechDate", "{0:dd MMM yyyy}") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Non-Tech Instructions:</td>
                        <td>


                            <asp:TextBox ID="NonTechInstructionstextarea" runat="server" 
                                TextMode="MultiLine" Text='<%# Bind("NonTechInstructions") %>'></asp:TextBox>

                        </td>
                    </tr>

                </tbody>
            </table>
          </div>


          <div class="nopad">
            <table id="Ownership" class="table table-bordered span12">
                <thead>
                    <tr>
                        <th colspan="2" class="span12">Ownership</th>
                    </tr>
                </thead>
                <tbody>

                    <tr>
                        <td class="span6">Sustained By:</td>
                        <td class="span6">

                            <asp:DropDownList ID="DropDownList_SustainedBy" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="RoleList" DataTextField="RoleName" DataValueField="RoleId"  
                                SelectedValue='<%# Bind("SustainedBy") %>'>
                                <asp:listitem value="" text="Select a group"></asp:listitem>
                            </asp:DropDownList>


                        </td>
                    </tr>
                    <tr>
                        <td>Owned By:</td>
                        <td>    
                            <asp:RequiredFieldValidator ID="OwnerdByValidator" runat="server" ErrorMessage="Owned By Required" ControlToValidate="DropDownList_OwnedBy" CssClass="ValidationError" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
                            <asp:DropDownList ID="DropDownList_OwnedBy" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="RoleList" DataTextField="RoleName" DataValueField="RoleId"  
                                SelectedValue='<%# Bind("OwnedBy") %>'>
                                <asp:listitem value="" text="Select a group"></asp:listitem>
                            </asp:DropDownList>                     
                        </td>
                    </tr>


                    <tr>
                        <td>Signed Out By:</td>
                        <td>
                            <asp:RequiredFieldValidator ID="SignedOutByValidator" runat="server" ErrorMessage="Signed Out By Required" ControlToValidate="DropDownList_SignedOutBy" CssClass="ValidationError" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
                            <asp:DropDownList ID="DropDownList_SignedOutBy" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="ProfileList" DataTextField="UserName" DataValueField="UserId"  
                                SelectedValue='<%# Bind("SignedOutBy") %>'>
                                <asp:listitem value="" text="Select a person"></asp:listitem>
                            </asp:DropDownList>                         
                        </td>
                    </tr>
                    <tr>
                        <td>Location:</td>
                        <td>
                            <asp:TextBox ID="LocationTextBox" runat="server" maxlength="50" 
                            Text='<%# Bind("Location") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Loaned To:</td>
                        <td>    
                             <asp:DropDownList ID="DropDownList_LoanedTo" runat="server" 
                                OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true" 
                                DataSourceID="ProfileList" DataTextField="UserName" DataValueField="UserId"  
                                SelectedValue='<%# Bind("LoanedTo") %>'>
                                <asp:listitem value="" text="Select a person"></asp:listitem>
                            </asp:DropDownList>                         
                        </td>
                    </tr>
                    <tr>
                        <td>Loaned To Location:</td>
                        <td>
                            <asp:TextBox ID="LoanedToLocationTextBox" runat="server" maxlength="50" 
                            Text='<%# Bind("LoanedToLocation") %>' />                           
                        </td>
                    </tr>
                    <tr>
                        <td>Expected Loan Return Date:</td>
                        <td>
                            <asp:TextBox ID="ExpectedLoanReturnDate_aspnetdatepicker" runat="server" 
                            Text='<%# Bind("ExpectedLoanReturnDate", "{0:dd MMM yyyy}") %>' />                           
                        </td>
                    </tr>



                </tbody>
            </table>


                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CssClass="btn marginR10 marginB10" 
                    CommandName="Update" Text="Update" />
                    <asp:LinkButton ID="UpdateCancelButton" runat="server" CssClass="btn marginR10 marginB10" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />  
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:LinkButton ID="DeleteButton_aspnetSimpleConfirmDelete" runat="server" OnClick="DeleteButton_aspnetSimpleConfirmDelete_Click" CausesValidation="False" CssClass="btn btn-danger marginR10 marginB10"  
                        OnClientClick="return confirm('Are you sure you want to delete this record?');" 
                        CommandName="Delete" Text="<span class='icomoon-icon-remove white'></span> Delete " />


        </EditItemTemplate>

    </asp:FormView>
    <asp:SqlDataSource ID="ProfileList" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT UserId, UserName FROM [aspnet_Users] ORDER BY UserName ASC"></asp:SqlDataSource>
    <asp:SqlDataSource ID="RoleList" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT [RoleName], [RoleId] FROM [vw_aspnet_Roles]"></asp:SqlDataSource>
    <br />

</div>

2 个答案:

答案 0 :(得分:0)

数据库是否设计了参照完整性?如果没有,请这样做。 然后只需触发delete命令,并实现SqlDataSource.Deleted事件。您将找到一个事件参数,该参数包含一个例外,以防参照完整性拒绝删除记录。然后您可以通知您的用户。

答案 1 :(得分:0)

如果相关表具有与父表相关的id,则可以在删除命令之前检查子表的记录是否存在。如果存在,则可以通知用户存在哪个值。