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


writing asp.net app using vb. bll uses adapter.update insert new reocrds. turn calls stored procedure, returns meaningful messages (e.g. "this invoice exists") want display user.

i can't figure out how message though.

here's part of bll insert function:

    <system.componentmodel.dataobject()> _ public class invoicesbll     private _invoiceadapter shipping_invoicetableadapter = nothing     protected readonly property adapter() shipping_invoicetableadapter                     if _invoiceadapter nothing                 _invoiceadapter = new shipping_invoicetableadapter()             end if             return _invoiceadapter         end     end property  ' insert new invoice         <system.componentmodel.dataobjectmethodattribute(system.componentmodel.dataobjectmethodtype.insert, true)> _         public function addinvoice( _                                    byval tripno string, _                                    byval typeid integer, _                                    byval vendorid integer, _                                    byval invno string, _                                    byval invamount decimal, _                                    byval invdate date, _                                    byval id integer _                                    ) boolean               dim invoices new acmeshipping.shipping_invoicedatatable()             dim invoice 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 integer = adapter.update(invoices)             return rowsaffected         end function 

i changed data force error on next insert , sqlexception. hoping there error property or adapter or something, don't see it.

the error forced 1 below, looks can system.data.sqlclient.sqlexception doesn't seem have relevant properties or method

system.data.sqlclient.sqlexception unhandled user code   class=16   errorcode=-2146232060   hresult=-2146232060   linenumber=148   message=insert shipping costs: subquery returned more 1 value. not permitted when subquery follows =, !=, <, <= , >, >= or when subquery used expression. - errorno: 512   number=50000   procedure=shipping_invoice_insertinvoice   server=10.60.2.141,2433   source=.net sqlclient data provider   state=1   stacktrace:        @ system.data.common.dbdataadapter.updatedrowstatuserrors(rowupdatedeventargs rowupdatedevent, batchcommandinfo[] batchcommands, int32 commandcount)        @ system.data.common.dbdataadapter.updatedrowstatus(rowupdatedeventargs rowupdatedevent, batchcommandinfo[] batchcommands, int32 commandcount)        @ system.data.common.dbdataadapter.update(datarow[] datarows, datatablemapping tablemapping)        @ system.data.common.dbdataadapter.updatefromdatatable(datatable datatable, datatablemapping tablemapping)        @ system.data.common.dbdataadapter.update(datatable datatable)        @ 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        @ 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 point me in right direction detecting specific exceptions when using adapter.update?

so figured out message is, don't why still applicationexception unhandled

my iteminserted event looks this:

protected sub invoiceinserted(sender object, e detailsviewinsertedeventargs) handles dvinvoice.iteminserted          if e.exception isnot nothing             exceptiondetails.visible = true             exceptiondetails.text = "there problem saving invoice. "             if e.exception.innerexception isnot nothing                 dim inner exception = e.exception.innerexception                 if typeof inner system.data.common.dbexception                     exceptiondetails.text &= _                     "insert failed." & _                     "please try again later."                 elseif typeof inner _                  system.data.nonullallowedexception                     exceptiondetails.text += _                         "there 1 or more required fields missing."                 elseif typeof inner _              system.data.sqlclient.sqlexception                     exceptiondetails.text += _                         "insert failed." & e.exception.message                 elseif typeof inner argumentexception                     dim paramname string = ctype(inner, argumentexception).paramname                     exceptiondetails.text &= _                         string.concat("the ", paramname, " value illegal.")                 elseif typeof inner applicationexception                     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 

but when run it, keep getting message applicationexception unhandled in user code. however, exceptiondetails display message. surely indicates it's handled?

what missing thanks

mark

look @ stack trace:

the error occurred in method addinvoice on line 76 of invoicesbll.vb file.

update:

add try-catch blocks around code want handle exceptions for, this:

try     ' database call here catch nullex nonullallowedexception     ' put logic here nonullallowedexception catch dbex dbexception     ' put logic here dbexception catch sqlex sqlexception     ' put logic here sqlexception catch appex applicationexception     ' put logic here applicationexception catch ex exception     ' catch-all block, because exceptions derive system.exception class end try 

note: multiple catch first 1 meets criteria of exception thrown match , others not evaluated. means want put more specific exception types (nonullallowedexception) earlier in list of catch blocks versus more general exception types (exception).


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -