0

使用 VB 编写 ASP.NET 应用程序。我的 BLL 使用 Adapter.Update 插入新的记录。这又调用了一个存储过程,它返回我想要显示给用户的有意义的消息(例如“这张发票已经存在”)。

我不知道如何获取消息。

这是我的带有插入功能的 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:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\372b2bbc\295d1b24\App_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 Documents\Visual Studio 2012\Projects\SegerdahlShippingInvoices\trunk\App_Code\BLL\InvoicesBLL.vb:line 76
  InnerException: 

有人可以指出我在使用 adapter.update 时检测特定异常的正确方向吗?

所以我弄清楚了消息在哪里,但不明白为什么我仍然没有处理 ApplicationException

我的 ItemInserted 事件如下所示:

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

但是当我运行它时,我不断收到有关用户代码中未处理的 ApplicationException 的消息。但是,ExceptionDetails 会显示该消息。当然,这表明它已被处理?

我错过了什么谢谢

标记

4

1 回答 1

1

查看堆栈跟踪:

错误发生在文件AddInvoice第 76 行的方法中InvoicesBLL.vb

更新:

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)。

于 2013-08-07T16:27:46.567 回答