|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do I catch duplicate record exception?Hello All,
How do I catch duplicate record attempted to be added exception? As in: Catch ex As Exception Thanks, Hexman Catch ex As Exception
If InStr(1, ex.ToString, "duplicate key") > 0 Then lblMessage.Text = " User already exists cannot insert duplicate value." Else lblMessage.Text = ex.ToString() End If Finally 'close the connection here, the Finally part will always execute Conn.Close() Hexman wrote: Show quoteHide quote > Hello All, > > How do I catch duplicate record attempted to be added exception? As in: > > Catch ex As Exception > > > Thanks, > > Hexman Thanks Scorpion,
I just thought there would be a error number that could be tested for this condition, like H_Result. Hexman Show quoteHide quote On 20 Aug 2006 05:14:35 -0700, "scorpion53061" <ad***@kjmsolutions.com> wrote: >Catch ex As Exception > If InStr(1, ex.ToString, "duplicate key") > 0 Then > > lblMessage.Text = " User already exists cannot insert >duplicate value." > > Else > > lblMessage.Text = ex.ToString() > > End If > > Finally > 'close the connection here, the Finally part will always >execute > Conn.Close() >Hexman wrote: >> Hello All, >> >> How do I catch duplicate record attempted to be added exception? As in: >> >> Catch ex As Exception >> >> >> Thanks, >> >> Hexman Hexman wrote:
> How do I catch duplicate record attempted to be added exception? As in: Start with this, generate the Exception and see what you get.> > Catch ex As Exception ? ex.GetType().ToString() Assuming this isn't just System.Exception, then change the Catch to catch the required Type and examine each property of that subclass, something like Catch ex As ODBCException ? ex.Errors.Count ? ex.InnerException.ToString() and so on. If you're lucky, you'll find something that identifies a duplicate record exception explicitly. If not, you'll have to fall back on looking for some text in the Message. This will work but isn't ideal; the message generated could change with, say, an upgrade to the DBMS software, thereby breaking your program. HTH, Phill W.
Late Binding Issue
string reset? why? how? Replace All XML Data Decimal Degrees to DMS Need some advice? What is the .NET way? - Right("0" & Now.Month.ToString, 2) Very large string HTTPS And VB.Net Apps Concurrency violation: the DeleteCommand affected 0 records vb.net Web Service using stored procedures example? |
|||||||||||||||||||||||