tags:

views:

41

answers:

2

Hello,

I have one dataset with a few tables. In one of the tables where I'm adding to, I would like to have a constraint where it will only allow to add a row if I have a unique value in a column.

Table: Users Columns: AutoID, GroupID, UnitID*, etc.

In the MVS2008, I went to the Designer and set the Unique property for UnitID to true, but it didn't work.

I've also hard-coded where I add a row to: this.dataset.Users.UnitIDColumn.Unique = true and tried a try/catch(InvalidConstraintException), but whenever I hard-code it in, I always get an exception even if the value is unique or not

How do I limit only adding a row with a unique UnitID? Thanks in advance.

A: 

Per MSDN

private void AddConstraint(DataTable table)
{
    UniqueConstraint uniqueConstraint;
    // Assuming a column named "UniqueColumn" exists, and 
    // its Unique property is true.
    uniqueConstraint = new UniqueConstraint(
        table.Columns["UniqueColumn"]);
    table.Constraints.Add(uniqueConstraint);
}
Roadie57
Thanks, this seemed to work. However, if you put this in the main code where we're adding the row, you'll get an exception saying that the constraint already exists. I did a simple check if it exists, remove it and then add it in again.
John H.
I would think that the constraints add would have to occur at table creation level and not at the row creation level. Haven't seen your code but I would assume you could move the add constraint above the row loop
Roadie57
per MSDN you can add Unique Constraints on multiple columns UNIQUE ConstraintsYou can use UNIQUE constraints to make sure that no duplicate values are entered in specific columns that do not participate in a primary key. Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of a column, or combination of columns, that is not the primary key. Multiple UNIQUE constraints can be defined on a table, whereas only one PRIMARY KEY constraint can be defined on a table.
Roadie57
A: 

I believe you can achieve that by designating a column as primary key in the DataSet designer.

I have tried in the DataSet designer to add a DataTable called Test with 2 columns, one of them is called ID which is a primary key. Just select it, right click and select "Set as Primary Key"
When I tried the following code


PharmacyDataSet.TestDataTable table = new PharmacyDataSet.TestDataTable();
table.AddTestRow(1, "one");
table.AddTestRow(1, "oneagain");
I got System.Data.ConstraintException {"Column 'ID' is constrained to be unique. Value '1' is already present."}

The code generated by the designer is


this.Constraints.Add(new global::System.Data.UniqueConstraint("Constraint1", new global::System.Data.DataColumn[] {this.columnID}, true));
this.columnID.Unique = true;

sh_kamalh
Thanks for your reply. However, I forgot to mention that I already have a Primary Key set. I was somehow able to set another key for a bit but I can't make it primary because there are other tables linked to it.
John H.