使用 VB 编写 ASP.NET 应用程序。My BLL 使用 Adapter.Update 插入新的 reocrds。这是轮到调用一个存储过程,它返回我要向用户显示的有意义的消息(例如"此发票已存在"(。
不过我不知道如何获取消息。
这是我带有插入功能的 BLL 的一部分:
<System.ComponentModel.DataObject()> _
Public Class InvoicesBLL
Private _invoiceAdapter As Shipping_InvoiceTableAdapter = Nothing
Protected ReadOnly Property Adapter() As Shipping_InvoiceTableAdapter
Get
If _invoiceAdapter Is Nothing Then
_invoiceAdapter = New Shipping_InvoiceTableAdapter()
End If
Return _invoiceAdapter
End Get
End Property
' Insert new Invoice
<System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, True)> _
Public Function AddInvoice( _
ByVal TripNo As String, _
ByVal TypeID As Integer, _
ByVal VendorID As Integer, _
ByVal InvNo As String, _
ByVal InvAmount As Decimal, _
ByVal InvDate As Date, _
ByVal ID As Integer _
) As Boolean
Dim invoices As New AcmeShipping.Shipping_InvoiceDataTable()
Dim invoice As AcmeShipping.Shipping_InvoiceRow = invoices.NewShipping_InvoiceRow()
invoice.TripNo = TripNo
invoice.TypeID = TypeID
invoice.VendorID = VendorID
invoice.InvNo = InvNo
invoice.InvAmount = InvAmount
invoice.InvDate = InvDate
invoices.AddShipping_InvoiceRow(invoice)
Dim rowsAffected As Integer = Adapter.Update(invoices)
Return rowsAffected
End Function
我更改了一些数据以在下次插入时强制出错,并且出现 sqlexception。我希望会出现错误属性或适配器或其他东西,但我没有看到它。
我强制的错误是下面的错误,看起来我可以从System.Data.SqlClient.SqlException中获取它,但这似乎没有任何相关的属性或方法
System.Data.SqlClient.SqlException was unhandled by user code
Class=16
ErrorCode=-2146232060
HResult=-2146232060
LineNumber=148
Message=Insert Shipping Costs: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. - ErrorNo: 512
Number=50000
Procedure=Shipping_Invoice_InsertInvoice
Server=10.60.2.141,2433
Source=.Net SqlClient Data Provider
State=1
StackTrace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at AcmeShippingTableAdapters.Shipping_InvoiceTableAdapter.Update(Shipping_InvoiceDataTable dataTable) in C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Filesroot372b2bbc295d1b24App_Code.6ff9nff6.5.vb:line 4136
at InvoicesBLL.AddInvoice(String TripNo, Int32 TypeID, Int32 VendorID, String InvNo, Decimal InvAmount, DateTime InvDate, Int32 ID) in S:My DocumentsVisual Studio 2012ProjectsSegerdahlShippingInvoicestrunkApp_CodeBLLInvoicesBLL.vb:line 76
InnerException:
有人可以指出我在使用 adapter.update 时检测特定异常的正确方向吗?
所以我弄清楚了消息在哪里,但不明白为什么我仍然没有得到应用程序异常未处理
My ItemInsert 事件如下所示:
Protected Sub InvoiceInserted(sender As Object, e As DetailsViewInsertedEventArgs) Handles dvInvoice.ItemInserted
If e.Exception IsNot Nothing Then
ExceptionDetails.Visible = True
ExceptionDetails.Text = "There was a problem saving the invoice. "
If e.Exception.InnerException IsNot Nothing Then
Dim inner As Exception = e.Exception.InnerException
If TypeOf inner Is System.Data.Common.DbException Then
ExceptionDetails.Text &= _
"Insert Failed." & _
"Please try again later."
ElseIf TypeOf inner _
Is System.Data.NoNullAllowedException Then
ExceptionDetails.Text += _
"There are one or more required fields that are missing."
ElseIf TypeOf inner _
Is System.Data.SqlClient.SqlException Then
ExceptionDetails.Text += _
"Insert Failed." & e.Exception.Message
ElseIf TypeOf inner Is ArgumentException Then
Dim paramName As String = CType(inner, ArgumentException).ParamName
ExceptionDetails.Text &= _
String.Concat("The ", paramName, " value is illegal.")
ElseIf TypeOf inner Is ApplicationException Then
ExceptionDetails.Text += inner.Message
End If
Else
ExceptionDetails.Text += e.Exception.Message
End If
e.ExceptionHandled = True
e.KeepInInsertMode = True
Else
ExceptionDetails.Visible = True
ExceptionDetails.Text = "inserted. "
End If
gvCosts.DataBind()
End Sub
但是当我运行它时,我不断收到有关用户代码中未处理的应用程序异常的消息。但是,异常详细信息确实会显示该消息。这肯定表明它已处理吗?
我错过了什么谢谢
马克
查看堆栈跟踪:
错误发生在InvoicesBLL.vb
文件第 76 行的方法 AddInvoice
中。
更新:
在要处理异常的代码周围添加Try-Catch
块,如下所示:
Try
' Do database call here
Catch nullEx As NoNullAllowedException
' Put logic here for NoNullAllowedException
Catch dbEx As DbException
' Put logic here for DbException
Catch sqlEx As SqlException
' Put logic here for SqlException
Catch appEx As ApplicationException
' Put logic here for ApplicationException
Catch ex As Exception
' This is a catch-all block, because all exceptions derive from System.Exception class
End Try
注意:对于多个Catch
第一个满足引发异常条件的将匹配,而其他将不会被评估。这意味着您希望将更具体的异常类型(NoNullAllowedException
(放在Catch
块列表的前面,而不是更通用的异常类型(Exception
(。