Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 21st, 2004, 06:35 AM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to test for primary key exception?

Hi Folks,

Can anybody tell me how to test for a primary key constraint exception?

I'm running a stored procedure, using sqlclient.executenonquery, and then testing for an exception. However, if it is specifically a primary key violation, I need to record this fact. In the olden days, it was a simple matter of checking the error number - what does one do now?
 
Old October 21st, 2004, 07:01 AM
Authorized User
 
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I used this code to check for a unique constraint voliation, whether it is best practices I'm not sure, I couldn't find another way at the time.

 Catch ex As SqlException
            'catch any errors
            If ex.Number = 2627 Then 'catch error unique index contraint violated
                MessageBox.Show(ex.message,Application.ProductName , MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If



Duncan
 
Old October 21st, 2004, 07:05 AM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thats what I was thinking of, but it says "number is not a member of system.exception"
 
Old October 21st, 2004, 07:08 AM
Authorized User
 
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

It would be sqlException and not exception

Duncan
 
Old October 21st, 2004, 07:13 AM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

given that the code i've got is

Try
    CmdInsert.ExecuteNonQuery()
Catch ex As Exception

how do I test sqlexception?
 
Old October 21st, 2004, 07:21 AM
Authorized User
 
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

the exception handles all exceptions thrown, you an change the type of exception by removing the word exception as you do intellisense will then prompt you, for more information on exceptions look up exceptions in the help and msdn also on the debug menu there is an exceptions control, this basicly tell VS.net what to do when it comes across an exception, best not to change these settings until you are familar with exceptions.



Duncan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Primary key cf2006 BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 31st, 2006 07:21 PM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.