Question : Unique constraint on dataset

Hi,

I have the following code where i am inserting data into database.
Before inserting, i am checking the data in dataset for duplicates.
If there are duplicates, i want to prevent the save.
So, i created a constraint for the dataset and want to catch the exception if the key constraint is violated.
Any time there is duplicate record the exception has to be caught in catch block(Constraint exception).
It is being caught in the end catch block.
Why is it not being caught in the catch block for constraint exception

Thanks in Advance
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Try
                builder.GetUpdateCommand()
                myDataAdapter.UpdateCommand = builder.GetUpdateCommand()
                ds.Tables(0).Columns(1).Unique = True
                Dim myUC As UniqueConstraint
                Dim mytable As DataTable = ds.Tables(0)
                Try
                    myUC = New UniqueConstraint("UC", ds.Tables(0).Columns(1))
                    'ds.Tables(0).Constraints.Add(myUC)
 		Catch ukex As ConstraintException
                MessageBox.Show("Column contents are not Unique", "Duplicate Data", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                'Catch uniqEx As Exception
                    'MessageBox.Show(uniqEx.Message)
                    Exit Sub
                End Try
                myDataAdapter.Update(ds, "myDataset")
            	MessageBox.Show("Your data is Saved", "Save Successful!")
            	Catch ex As Exception
           	MessageBox.Show("Name:  saveData" + vbCrLf + "Error Message: " + ex.ToString)
        	End Try

Answer : Unique constraint on dataset

In your first section, you actually state Unique = True...so this sets the constraint.  In the second one, you are creating the constraint and setting it up, but you have to say ds.EnforceConstraints=True.  At that point it should, should kivk out your exception.

Random Solutions  
 
programming4us programming4us