无法捕获gridview异常

时间:2015-04-07 16:29:37

标签: c# asp.net database gridview

我正在尝试在更新gridview时捕获异常。 代码是

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",table" dbo.Suppliers",column' SupplierID'中。 该语句已终止。 它曾经工作过一次,但每次都没有工作。 而且我也得到了Asp.net验证viewstate MAC失败的错误。

1 个答案:

答案 0 :(得分:0)

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