471,325 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Datatable Primary Key not being set from Xml Schema

Hi there,

I have created an XmlSchema to represent the tables in my database and
I have specified a primary key for each of the tables.

However, to be able to execute the following code:

DatabaseTables.CategoryInfoRow categoryRow =
m_dtCategories.Rows.Find(selected.category_ID) as

I need to explicitly reassign the primary keys for the table with this
as it throws an error saying no primary key defined:

// Specify the primary key columns
DataColumn[] keys = new DataColumn[1];
keys[0] = m_dtCategories.Columns["Category_ID"];
m_dtCategories.PrimaryKey = keys;

Can anyone suggest what has gone wrong with my schema or was this
always the way we had to do it? I didn't manually write the xml for the
schema, I did it through setting properties in the data view but here
is what it generated for my table in question:

<xs:element name="CategoryInfo">
<xs:element name="Category_ID" type="xs:int"
msdata:AutoIncrement="true" />
<xs:element name="Parent_Cat_ID" type="xs:int" />
<xs:element name="Category_Name" type="xs:string" />
<xs:key name="PK_CategoryInfo" msdata:PrimaryKey="true">
<xs:selector xpath="." />
<xs:field xpath="mstns:Category_ID" />



Jan 18 '06 #1
6 8756

I think the problem lies in the <xs:selector xpath="."/> element. The
xpath attribute of xs:selector should point to the table that the key
is assigned to, which in this case is pointing to the root element '.'.
Change the xpath attribute value to ".//mstns:CategoryInfo", save and
then retry your code. The resulting schema should be like this:

<xs:key name="PK_CategoryInfo" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:CategoryInfo" /> <!--
Changed value -->
<xs:field xpath="mstns:Category_ID" />
If that doesnt work could you please post the exception that you are

- NuTcAsE

Jan 18 '06 #2
Thanks for the reply. I made the change in the xsd file and rebuilt my
project without the lines of code that explicitly set the Primary Keys
column details.

Here is the exception I get when I attempt to do a find of the rows:

An unhandled exception of type 'System.Data.MissingPrimaryKeyException'
occurred in system.data.dll

Additional information: Table doesn't have a primary key.""

When I look at the table in the watch window, the primary key attribute
has a length of 0 and is set to an undefined value. Is it supposed to
be reading this from the xml schema file when it creates the typed data
table instance or is the primary key verified against the actual table
in the database where I load the data into the data table?



Jan 18 '06 #3
I don't know if this will help identify what I have done incorrectly.
When I went back to the data view of the schema after making your
suggested change, the little blue key was missing from my row. Though
in the properties box for the element, it still had details of the

Jan 18 '06 #4
<?xml version="1.0" encoding="utf-8" ?>

I tried creating a data set with the schema definition you supplied
earlier, and this is what the xsd file contained. It created the data
set correctly, and I could use the Find function without assigning the
keys manually (FYI: If the Primary key in the xsd file is setup
properly then there should be a FindByCategory_ID functiont that
returns a typed CategoryInfoRow instance).

Can you post the entire schema definition of the xsd file or check the
differences in your file with this. If that doesnt work, I suggest
replacing the CategoryInfo element and the Primary Key definition in
your xsd with the sample below. If that doesnt work, could you post
your xsd file definition?

<xs:schema id="Test" targetNamespace="http://tempuri.org/Test.xsd"
<xs:element name="Test" msdata:IsDataSet="true">
<xs:choice maxOccurs="unbounded">
<xs:element name="CategoryInfo">
<xs:element name="Category_ID"
type="xs:int" minOccurs="0" />
<xs:element name="Parent_Cat_ID"
type="xs:int" minOccurs="0" />
<xs:element name="Category_Name"
type="xs:string" minOccurs="0" />
<xs:key name="CategoryInfo_PK" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:CategoryInfo" />
<xs:field xpath="mstns:Category_ID" />

Hope this helps.

- NuTcAsE

Jan 18 '06 #5

Thanks for the help. I recreated my whole schema file based on the
excerpt above and it now seems to have created the primary keys and I
now have the FindBy_ID() functions available.

I think my problem was that I didn't have an element with dataSet=true
that housed all the other elements representing my tables.

I was missing:

<xs:element name="Test" msdata:IsDataSet="true">
<xs:choice maxOccurs="unbounded">

I don't fully understand what they are doing so I'll do some more
reading on XmlSchemas. I can't understand how I got into this mess in
the first place. I chose not to hand write the schema because of my
lack of knowledge and instead opted to use the Schema design page and
there I just right clicked and created new elements with nothing else.
I don't know how I could have told it to do all the segments that you
created for me.

My project no longer builds now because the schema has completely
changed, so I have some work to do now to get that building. But thanks
ever so much for all the help.


Jan 18 '06 #6
If you used the data set schema designer it should have placed the
msdata:IsDataSet="true" automatically on the root data set element. The
only way (if using schema designer ONLY), that attribute value is
removed or set to false is if in the property pages of the dataset
schema the IsDataSet is set to false.

- NuTcAsE

Jan 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Peter | last post: by
1 post views Thread by Rathtap | last post: by
10 posts views Thread by Bernie Yaeger | last post: by
1 post views Thread by kempshall | last post: by
2 posts views Thread by =?Utf-8?B?Sm9iIExvdA==?= | last post: by
15 posts views Thread by Joseph Geretz | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.