Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to set up a UNIQUE Constraint

Author  Topic 

new2this
Starting Member

16 Posts

Posted - 2008-10-02 : 00:46:17
I am trying to set up a Unique Constraint for a column in my database. I am using the server explorer to set this up but I think I am doing something wrong because when I run the application I have connected to the database it is the SqlException that catches the exception.

Here is code from my program:

Catch ex As ConstraintException
MessageBox.Show(Me.xCreditCardNameTextBox.Text & " is already in the database.", "Add Credit Card", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As DataException
MessageBox.Show(ex.Message, ex.GetType.ToString)
Catch ex As SqlException
MessageBox.Show("Database error #" & ex.Number & ControlChars.NewLine & "Message: " & ex.Message, _
ex.GetType.ToString)

I am using Manage Keys/Indexes and selecting the column I want to be unique. Then I switch is Unique to true. And the type I am setting to Unique Key. Is this correct if I want the column to be unique? My problem may be the code in the catch. How can i catch this exception?

Thanks.
   

- Advertisement -