无法捕获gridview异常(Not able to catch gridview exception)

我正在尝试在更新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”,表“dbo.Suppliers”,列“SupplierID”中。 该语句已终止。 它曾经工作过一次,但每次都没有工作。 我也得到了Asp.net验证viewstate MAC失败的错误。

I am trying to catch exception while updating a gridview. code is

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;

and code for exception handling is

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"; }

but still i am getting error :The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Products_Suppliers". The conflict occurred in database "NORTHWIND", table "dbo.Suppliers", column 'SupplierID'. The statement has been terminated. it had worked once ,but not working everytime. And i am also getting Asp.net Validation of viewstate MAC failed error.

最满意答案

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

The exception is happening in the stored procedure being called in UpdateProduct so the event RowUpdated is never reached/called so you cannot "catch" that exception there.

无法捕获gridview异常(Not able to catch gridview exception)

我正在尝试在更新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”,表“dbo.Suppliers”,列“SupplierID”中。 该语句已终止。 它曾经工作过一次,但每次都没有工作。 我也得到了Asp.net验证viewstate MAC失败的错误。

I am trying to catch exception while updating a gridview. code is

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;

and code for exception handling is

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"; }

but still i am getting error :The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Products_Suppliers". The conflict occurred in database "NORTHWIND", table "dbo.Suppliers", column 'SupplierID'. The statement has been terminated. it had worked once ,but not working everytime. And i am also getting Asp.net Validation of viewstate MAC failed error.

最满意答案

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

The exception is happening in the stored procedure being called in UpdateProduct so the event RowUpdated is never reached/called so you cannot "catch" that exception there.