473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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
DatabaseTables.CategoryInfoRow;

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:complexType>
<xs:sequence>
<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:sequence>
</xs:complexType>
<xs:key name="PK_CategoryInfo" msdata:PrimaryKey="true">
<xs:selector xpath="." />
<xs:field xpath="mstns:Category_ID" />
</xs:key>
</xs:element>

Thanks,

Emma

Jan 18 '06 #1
6 8925
Hi,

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" />
</xs:key>
If that doesnt work could you please post the exception that you are
getting?

- 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?

Thanks,

Emma

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
key....

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

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"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns="http://tempuri.org/Test.xsd"
xmlns:mstns="http://tempuri.org/Test.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Test" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="CategoryInfo">
<xs:complexType>
<xs:sequence>
<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:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="CategoryInfo_PK" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:CategoryInfo" />
<xs:field xpath="mstns:Category_ID" />
</xs:key>
</xs:element>
</xs:schema>

Hope this helps.

- NuTcAsE

Jan 18 '06 #5
Hi,

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:complexType>
<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.

Emma

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter | last post by:
Hi, I have two datatables and I want to efficiently query the intersection of the two using the studentID. In my example code below, I want to print out Mary and Jane and their exam grades and I...
1
by: Rathtap | last post by:
I read claims from a file into a DataTable and DataRow and then use the data adapter's Update method to push those claims into the database. I import a few thousand claims at a time and before...
1
by: Mike | last post by:
I have an ASP.NET/VB app that updates values in a DataTable over the course of about 3 different pages. On the way out of the first of these pages, I explicitly build the DataTable from values in...
10
by: Bernie Yaeger | last post by:
I have a need to add a primary key to a dataset/datatable. How can this be done using a standard oledb data provider? Tx for any help.
1
by: kempshall | last post by:
Hi, I have a question about the .NET DataTable.ImportRow method. If I import a DataRow into an empty DataTable, will the empty DataTable pick up the schema of the DataRow I'm trying to put into...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
2
by: =?Utf-8?B?Sm9iIExvdA==?= | last post by:
How can I reconcile changes between two data table and create a subset of those changes. I have two data tables with same schema which gets populated from two different xml files. I want to get...
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
3
by: szwejk | last post by:
Hi! I want to return DataTable from web service method. This method generate xml with schema and data. Is there any way to retrun more informations about columns? If I want to get maxLength...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.