[.net] Problems with XML Unique Constraint

Started by
3 comments, last by Zul 19 years, 8 months ago
Hey guys, Got a tiny problem that I can't seem to get around. I've got a StudentID field in a Student table, which is generated via an XML file. The DataSet works perfectly except that I can add duplicate StudentIDs to the table. After closing the application, it writes the new DataSet over the existing one stored in the XML file, and this is some of the output from that file:


  <Student StudentID="1" FirstName="asd" LastName="asd" Address="asd" City="asd" Course="11" Email="" Comments="" />
  <Student StudentID="1" FirstName="asd" LastName="asd" Address="asd" City="asd" Course="11" Email="" Comments="" />
  <Student StudentID="1" FirstName="asd" LastName="asd" Address="asd" City="asd" Course="11" Email="" Comments="" />

As you can see, I've got duplicates in here. I've got the StudentID setup like this in the schema file:

<xs:unique name="StudentKey">
	<xs:selector xpath=".//mstns:Student" />
	<xs:field xpath="@StudentID" />
</xs:unique>

A new student is added like so:

// Setup a new row for the new student.
DataRow newStudent = registerDataSet.Tables["Student"].NewRow();

...

// Add the new student to the table.
registerDataSet.Tables["Student"].Rows.Add(newStudent);

// Accept the changes.
newStudent.AcceptChanges();

Any ideas as to what I'm missing? Thanks in advance, -hellz
Advertisement
Check out this article

It looks like it might help or at least point you in the right direction. Basically, it seems like the problem is you're not specifying the unique constraint in your dataset, so you're getting the duplicates in there first, and then you're forcing them into the xml file that you write.

From the link:
Quote:
The key element specifies that the values of the CustomerID child element of the Customers element must have unique values and cannot have null values. In translating the XML Schema definition language (XSD) schema, the mapping process creates the following table:

Customers(CustomerID, CompanyName, Phone)

The XML Schema mapping also creates a UniqueConstraint on the CustomerID column, as shown in the following DataSet. (For simplicity, only relevant properties are shown.)

DataSetName: MyDataSet
TableName: customers
ColumnName: CustomerID
AllowDBNull: False
Unique: True
ConstraintName: KeyCustID
Table: customers
Columns: CustomerID
IsPrimaryKey: True

In the DataSet that is generated, the IsPrimaryKey property of the UniqueConstraint is set to True because the schema specifies msdata:PrimaryKey="true" in the key element.

The value of the ConstraintName property of the UniqueConstraint in the DataSet is the value of the msdata:ConstraintName attribute specified in the key element in the schema.


See if that doesn't take care of it.
oh hai
Hey Zul,

Thanks for the link and the info. I think I've already done that (tried so many things in 4 short hours [grin]), but I'm away from home at the moment, so I can't be sure.

Thanks for giving me the link, though. I'll try it out when I get home in about a week's time. [smile]

-hellz
May be just repeating the other post, but, as stated, sounds like you need to create a constraint on the data table.

Example of this:
DataTable dtStudent = registerDataSet.Tables["Student"];UniqueConstraint studentUC = new UniqueConstraint(new DataColumn[]                               {dtStudent.Columns["StudentId"]});registerDataSet.Tables["Student"].Constraints.Add(custUC);


Further, you also need to increment the StudentId somewhere. Just creating a unique constraint just prevents duplicates (and will only complain about it when you go to AcceptChanges()).

Let us all know how you get along.
Quote:Original post by hellz
Hey Zul,

Thanks for the link and the info. I think I've already done that (tried so many things in 4 short hours [grin]), but I'm away from home at the moment, so I can't be sure.

Thanks for giving me the link, though. I'll try it out when I get home in about a week's time. [smile]

-hellz


No problem. I've done a lot of wrestling with datasets and I know how frustrating they can be at times. Hope it fixes your problem.
oh hai

This topic is closed to new replies.

Advertisement