0

VB を使用して ASP.NET アプリを作成します。私の 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:\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 unhandled を取得するのかわかりません

私の 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 unhandled in user code に関するメッセージが表示され続けます。ただし、ExceptionDetails DOES はメッセージを表示します。確かにこれはそれが処理されたことを示していますか?

何が足りないのですか ありがとう

マーク

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のリストの前に配置する必要があることを意味します。CatchException

于 2013-08-07T16:27:46.567 に答える