【问题标题】:Not able to catch gridview exception无法捕获 gridview 异常
【发布时间】:2015-04-07 16:29:37
【问题描述】:

我试图在更新网格视图时捕获异常。 代码是

public static int UpdateProduct(
                             int productID,
                             string productName,
                             int supplierID,
                             int categoryID,
                             string quantityPerUnit,
                             decimal unitPrice,
                             int unitsInStock,
                             int unitsOnOrder,
                             int reorderLevel,
                             bool discontinued)
    {
        int rowsAffected = 0;

        using (SqlConnection connection = ConnectionManager.GetNorthwindConnection())
        {
              SqlCommand command = new SqlCommand("ttUpdateProduct", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@ProductID", SqlDbType.Int).Value = productID;
                command.Parameters.Add("@ProductName", SqlDbType.NVarChar, 40).Value = productName;
                command.Parameters.Add("@SupplierID", SqlDbType.Int).Value = supplierID;
                command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = categoryID;
                command.Parameters.Add("@QuantityPerUnit", SqlDbType.NVarChar, 20).Value = quantityPerUnit;
                command.Parameters.Add("@UnitPrice", SqlDbType.Money).Value = unitPrice;
                command.Parameters.Add("@UnitsInStock", SqlDbType.SmallInt).Value = unitsInStock;
                command.Parameters.Add("@UnitsOnOrder", SqlDbType.SmallInt).Value = unitsOnOrder;
                command.Parameters.Add("@ReorderLevel", SqlDbType.SmallInt).Value = reorderLevel;
                command.Parameters.Add("@Discontinued", SqlDbType.Bit).Value = discontinued;

                rowsAffected = command.ExecuteNonQuery();



        }
        return rowsAffected;
    using (SqlConnection connection = ConnectionManager.GetNorthwindConnection())
        {
              SqlCommand command = new SqlCommand("ttUpdateProduct", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@ProductID", SqlDbType.Int).Value = productID;
                command.Parameters.Add("@ProductName", SqlDbType.NVarChar, 40).Value = productName;
                command.Parameters.Add("@SupplierID", SqlDbType.Int).Value = supplierID;
                command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = categoryID;
                command.Parameters.Add("@QuantityPerUnit", SqlDbType.NVarChar, 20).Value = quantityPerUnit;
                command.Parameters.Add("@UnitPrice", SqlDbType.Money).Value = unitPrice;
                command.Parameters.Add("@UnitsInStock", SqlDbType.SmallInt).Value = unitsInStock;
                command.Parameters.Add("@UnitsOnOrder", SqlDbType.SmallInt).Value = unitsOnOrder;
                command.Parameters.Add("@ReorderLevel", SqlDbType.SmallInt).Value = reorderLevel;
                command.Parameters.Add("@Discontinued", SqlDbType.Bit).Value = discontinued;

                rowsAffected = command.ExecuteNonQuery();



        }
        return rowsAffected;

异常处理的代码是

protected void ProductGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
    if (e.Exception != null)
    {
        Master.ErrorMessage = "Cannot Update Record";
        e.ExceptionHandled = true;
    }
    else
    {
        Master.ResultMessage = "Record Updated Succesfully";
    }

但我仍然收到错误:UPDATE 语句与 FOREIGN KEY 约束“FK_Products_Suppliers”冲突。冲突发生在数据库“NORTHWIND”、表“dbo.Suppliers”、列“SupplierID”中。 该语句已终止。 它曾经工作过,但不是每次都工作。 而且我还收到 Asp.net Validation of viewstate MAC failed 错误。

【问题讨论】:

    标签: c# asp.net database gridview


    【解决方案1】:

    异常发生在 UpdateProduct 中调用的存储过程中,因此永远不会到达/调用事件 RowUpdated,因此您无法在那里“捕获”该异常。

    【讨论】:

    • 实际上它工作过一次,但再次开始显示此问题。并且已经检查了存储过程。问题是否有可能与 viewstate MAC failed 错误的验证有关。
    • 外键约束错误不是 ASP.NET 错误。 ASP 只返回调用 ExecuteNonQuery 提供的异常。要“捕获”更新行中的错误,您必须将 ExecuteNonQuery 调用包装在 try/catch 中以捕获异常并允许函数调用完成,以便触发 RowUpdated 事件
    • 捕获异常我应该做什么,因为我在类库文件中编写代码,所以我无法访问标签来显示消息。
    • 你没有做任何事情,也许记录错误,这取决于你的需要。 try/catch 的主要原因是捕获异常,因此它不会停止应用程序并允许函数完成,以便触发其余事件,特别是 RowUpdated。
    猜你喜欢
    • 2010-10-29
    • 1970-01-01
    • 2021-10-09
    • 2021-09-17
    • 2021-10-03
    • 2018-02-04
    • 2018-02-04
    • 2011-04-19
    相关资源
    最近更新 更多