Wednesday, 7 August 2013

How to Get SQL Error message from Adapter.Update (ASP.NET with VB)

How to Get SQL Error message from Adapter.Update (ASP.NET with VB)

Writing an ASP.NET app using VB. My BLL uses Adapter.Update to insert new
reocrds. This is turn calls a stored procedure, which returns meaningful
messages (e.g. "This invoice already exists") that I want to display to
the user.
I can't figure out how to get the message though.
Here's part of my BLL with insert function:
<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
I changed some data to force an error on next insert and I get an
sqlexception. I was hoping there would be an error property or the adapter
or something, but I don't see it.
The error I forced was the one below, it looks like I can get it from
System.Data.SqlClient.SqlException but that doesn't seem to have any
relevant properties or method
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:
Could someone point me in the right direction for detecting specific
exceptions when using adapter.update?
Thanks
Mark

No comments:

Post a Comment