Sign in to follow this  

[.net] Problems with XML Unique Constraint

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this