c# - 无法获取 RowUpdate 事件处理程序以显示 GridView 的错误

标签 c# asp.net

我遇到的问题是当我运行代码时遇到这个错误:

"Cannot insert the value NULL into column 'OnHand', table 'C:\USERS\UNKNOWN\DESKTOP\XEX14PRODUCTRECEIPT\XEX14PRODUCTRECEIPT\APP_DATA\HALLOWEEN.MDF.dbo.Products'; column does not allow nulls. UPDATE fails"

当我将 OnHand 值编辑为空值或 null 时会发生这种情况,但我应该得到我在 CodeBehind 中创建的错误“发生数据库错误”。

这是什么原因造成的?

谢谢!

GridView 代码隐藏

    public partial class Default : System.Web.UI.Page
{
    protected void grdProducts_SelectedIndexChanged(object sender, EventArgs e)
    {
        grdProducts.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
    protected void grdProducts_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            lblError.Text = "A databse error has occured. " + "Message: " + e.Exception.Message;
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;

        }
        else if (e.AffectedRows == 0)
        {
            lblError.Text = "Another user may have updated that category. " + "Please try again ";
        }
    }
}

Default.aspx

div class="container">
<header class="jumbotron"><%-- image set in site.css --%></header>
<main>
    <form id="form1" runat="server" class="form-horizontal">

        <div class="row">
            <div class="col-xs-12">
                <asp:GridView ID="grdProducts" runat="server" AllowPaging="True" 
                    AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" 
                    CssClass="table table-bordered table-condensed" OnSelectedIndexChanged="grdProducts_SelectedIndexChanged">
                    <Columns>
                        <asp:BoundField DataField="ProductID" HeaderText="ID" SortExpression="ProductID"
                            ReadOnly="True">
                            <HeaderStyle CssClass="col-sm-2" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="True">
                            <HeaderStyle CssClass="col-sm-6" />
                        </asp:BoundField>
                        <asp:BoundField DataField="OnHand" HeaderText="On Hand" SortExpression="OnHand">
                            <HeaderStyle CssClass="col-sm-2 text-right" />
                            <ItemStyle CssClass="text-right" />
                        </asp:BoundField>
                        <asp:CommandField ShowEditButton="True" />
                    </Columns>
                    <HeaderStyle CssClass="bg-halloween" />
                    <AlternatingRowStyle CssClass="altRow" />
                    <EditRowStyle CssClass="warning" />
                    <PagerStyle CssClass="bg-halloween" HorizontalAlign="Center" />
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:HalloweenConnection %>" 
                    SelectCommand="SELECT ProductID, Name, OnHand FROM Products" 
                    UpdateCommand="UPDATE Products SET OnHand = @OnHand WHERE (ProductID = @ProductID)">
                    <UpdateParameters>
                        <asp:Parameter Name="OnHand" />
                        <asp:Parameter Name="ProductID" />
                    </UpdateParameters>
                </asp:SqlDataSource>    
            </div>  
        </div>

        <div class="row">
            <div class="col-xs-12">
                <p><asp:Label ID="lblError" runat="server"  
                    CssClass="text-danger" EnableViewState="false"></asp:Label></p>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                    HeaderText="Please correct the following errors:" CssClass="text-danger" />
            </div>
        </div>
    </form>
</main>

最佳答案

由于引用完整性,或者因为该列不支持其中的信息为 NULL,因此通常在数据库中会发生类似的事情。

在关系型数据库中,您需要确保更新的信息有效,因为数据可能会从一个表转移到另一个表。在这种情况下,更改一个表的信息将更改许多表,并且具有 NULL 值有点像在多个表中有空白。

否则我不完全确定..您可能还想使用“try/catch” block 来捕获 Sqlexception 而不是 if/else。在 try catch block 中,您可以使用 if/else 来测试异常。

例如:

try
{
    //Whatever command you have executing your sql statement
}
catch(SqlException e)
{
    Console.WriteLine("An error occurred: " + e.ToString());
}

我会确保您有验证输入的代码,确保程序不会在未验证要插入数据库的信息类型是否正确的情况下尝试执行代码。

关于c# - 无法获取 RowUpdate 事件处理程序以显示 GridView 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584224/

相关文章:

c# - 使用 Razor 引擎的 ASP.NET MVC 3 中的 '/' 应用程序中的服务器错误

asp.net - ORA-12571 : TNS:packet writer failure with ASP. 网络

c# - 在日期中设置特定的一天

c# - TcpClient.GetStream().CopyTo(MemoryStream) 停止应用程序继续?

c# - 如何比较和存储比较元素形成一维和二维数组 c#?

c# - 显示带有静态标签的滚动网格

c# - 区域共享布局_ViewStart.cthml

c# - 应该如何编写一个抽象类来覆盖带有附加参数的方法

c# - 为什么程序使用添加到委托(delegate)的最后一个方法?

c# - 动态添加按钮到面板