473,699 Members | 2,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(selec ted.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_I D"];
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="CategoryI nfo">
<xs:complexType >
<xs:sequence>
<xs:element name="Category_ ID" type="xs:int"
msdata:AutoIncr ement="true" />
<xs:element name="Parent_Ca t_ID" type="xs:int" />
<xs:element name="Category_ Name" type="xs:string " />
</xs:sequence>
</xs:complexType>
<xs:key name="PK_Catego ryInfo" msdata:PrimaryK ey="true">
<xs:selector xpath="." />
<xs:field xpath="mstns:Ca tegory_ID" />
</xs:key>
</xs:element>

Thanks,

Emma

Jan 18 '06 #1
6 8963
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:CategoryI nfo", save and
then retry your code. The resulting schema should be like this:

<xs:key name="PK_Catego ryInfo" msdata:PrimaryK ey="true">
<xs:selector xpath=".//mstns:CategoryI nfo" /> <!--
Changed value -->
<xs:field xpath="mstns:Ca tegory_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.Mi ssingPrimaryKey Exception'
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"
elementFormDefa ult="qualified"
attributeFormDe fault="qualifie d"
xmlns="http://tempuri.org/Test.xsd"
xmlns:mstns="ht tp://tempuri.org/Test.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata">
<xs:element name="Test" msdata:IsDataSe t="true">
<xs:complexType >
<xs:choice maxOccurs="unbo unded">
<xs:element name="CategoryI nfo">
<xs:complexType >
<xs:sequence>
<xs:element name="Category_ ID"
type="xs:int" minOccurs="0" />
<xs:element name="Parent_Ca t_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="CategoryI nfo_PK" msdata:PrimaryK ey="true">
<xs:selector xpath=".//mstns:CategoryI nfo" />
<xs:field xpath="mstns:Ca tegory_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:IsDataSe t="true">
<xs:complexType >
<xs:choice maxOccurs="unbo unded">

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:IsDataSe t="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
9255
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 don't want to use loop because the real datatables are much larger. Can this be done using one of the inbuilt methods in the class library and if so how. Thank you in advance Peter DataTable examgrade;
1
12084
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 adding a claim to the DataTable I need to check if a claim with the same pk has already been added to the DataTable. If not I run a stored Proc to do the same check in the database. Pseudocode looks loke this:...
1
2969
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 a DataGrid, and set the PrimaryKey of the DataTable to be the first cell in the grid (which is a UserID value). I then store the DataTable in a session object, from which it is retrieved for subsequent pages. All this seems to be working fine. ...
10
27856
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
1927
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 it? For example, I have code that looks like this: DataTable tableWithData; DataTable emptyTable = new DataTable(); emptyTable.ImportRow(tableWithData.Rows);
9
3905
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 eg: area.txt, school.txt, students.txt.... and so on (ok?!?) now, 1. area code used in the school.txt must be defined in the area.txt (Primary key in area => area_code defined in area.txt & Foreign key on school => area_code defined in...
2
2497
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 hold of missing & changed rows in first table from second table. Tables have ID as primary key. Thanks
15
13509
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 http://msdn2.microsoft.com/en-us/library/ms996381.aspx Formerly, with classic Microsoft DNA architecture, the ADO Recordset was a primary transport medium, recommended for transmitting data between application tiers. In fact, there are whole books written on the subject.
3
2290
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 there is no problem because DataColum has property MaxLength, but I need more (e.g. color, mask..) Thx for help! -- Szwejk
0
8705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.