accessing typed dataset relationships?

D

Darren Clark

I am trying to understand typed datasets more.... and i am stuck on a little problem

I have created a type dataset called "News" this consists of 2 tables. NewsTable and NewsTypes.
the news has news_id int, title string and type int
the news type table has type_id int and type_name string
There is also a relationship created which is joining on the news.type and then newstype.type_id

In the code below i am putting in 2 rows for types... and 1 row for a news article.

My question is .... how can i get the STRING value of the newstype that relates to my news item? eg i want an easy way to get to the value "first type"

Any help would me much appreciated.

News myNews = new News();
News.NewsTypesRow tRow;

// Add some types
tRow = myNews.NewsTypes.NewNewsTypesRow();
tRow.type_id = 1;
tRow.type_name = "first type";

tRow.type_id = 2;
tRow.type_name = "second type";

// Add some datat to the news
News.NewsTableRow nRow = myNews.NewsTable.NewNewsTableRow();
nRow.title = "news title";
nRow.type = 1;
MessageBox.Show(nRow.title.ToString());
 
D

Darren Clark

Here is the XSD

<?xml version="1.0" encoding="utf-8" ?>

<xs:schema id="News" targetNamespace="http://tempuri.org/News.xsd" elementFormDefault="qualified"

attributeFormDefault="qualified" xmlns="http://tempuri.org/News.xsd" xmlns:mstns="http://tempuri.org/News.xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

<xs:element name="News" msdata:IsDataSet="true">

<xs:complexType>

<xs:choice maxOccurs="unbounded">

<xs:element name="NewsTable">

<xs:complexType>

<xs:sequence>

<xs:element name="news_id" type="xs:int" minOccurs="0" />

<xs:element name="title" type="xs:string" minOccurs="0" />

<xs:element name="type" type="xs:int" minOccurs="0" />

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="NewsTypes">

<xs:complexType>

<xs:sequence>

<xs:element name="type_id" type="xs:int" minOccurs="0" />

<xs:element name="type_name" type="xs:string" minOccurs="0" />

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:choice>

</xs:complexType>

<xs:key name="NewsKey1" msdata:primaryKey="true">

<xs:selector xpath=".//mstns:NewsTable" />

<xs:field xpath="mstns:news_id" />

</xs:key>

<xs:key name="NewsKey2">

<xs:selector xpath=".//mstns:NewsTypes" />

<xs:field xpath="mstns:type_id" />

</xs:key>

<xs:keyref name="NewsTypesNewsTable" refer="NewsKey2" msdata:ConstraintOnly="true">

<xs:selector xpath=".//mstns:NewsTable" />

<xs:field xpath="mstns:type" />

</xs:keyref>

</xs:element>

</xs:schema>

I am trying to understand typed datasets more.... and i am stuck on a little problem

I have created a type dataset called "News" this consists of 2 tables. NewsTable and NewsTypes.
the news has news_id int, title string and type int
the news type table has type_id int and type_name string
There is also a relationship created which is joining on the news.type and then newstype.type_id

In the code below i am putting in 2 rows for types... and 1 row for a news article.

My question is .... how can i get the STRING value of the newstype that relates to my news item? eg i want an easy way to get to the value "first type"

Any help would me much appreciated.

News myNews = new News();
News.NewsTypesRow tRow;

// Add some types
tRow = myNews.NewsTypes.NewNewsTypesRow();
tRow.type_id = 1;
tRow.type_name = "first type";

tRow.type_id = 2;
tRow.type_name = "second type";

// Add some datat to the news
News.NewsTableRow nRow = myNews.NewsTable.NewNewsTableRow();
nRow.title = "news title";
nRow.type = 1;
MessageBox.Show(nRow.title.ToString());
 
J

John Saunders

I am trying to understand typed datasets more.... and i am stuck on a little problem

I have created a type dataset called "News" this consists of 2 tables. NewsTable and NewsTypes.
the news has news_id int, title string and type int
the news type table has type_id int and type_name string
There is also a relationship created which is joining on the news.type and then newstype.type_id

In the code below i am putting in 2 rows for types... and 1 row for a news article.

My question is .... how can i get the STRING value of the newstype that relates to my news item? eg i want an easy way to get to the value "first type"

Any help would me much appreciated.

News myNews = new News();
News.NewsTypesRow tRow;

// Add some types
tRow = myNews.NewsTypes.NewNewsTypesRow();
tRow.type_id = 1;
tRow.type_name = "first type";
New<type>Row() just creates a new row object of the type. It doesn't add the new row to the DataSet. Try

myNews.NewsTypes.Add(tRow).

Also, you need another new row here:

tRow = myNews.NewsTypes.NewNewsTypesRow();

tRow.type_id = 2;
tRow.type_name = "second type";
myNews.NewsTypes.Add(tRow)
// Add some datat to the news
News.NewsTableRow nRow = myNews.NewsTable.NewNewsTableRow();
nRow.title = "news title";
nRow.type = 1;
myNews.NewsTable.Add(nRow);
 
D

Darren Clark

OK i have changed my code now to add the rows... but i still cant access the values in the other table..
It doesnt matter if i use getparentrow or getchildrows ... both fail..

however i know that the relationship works because if i try and add the value 5 to a news row it fails. and returns a FK error.

News myNews = new News();


News.NewsTypeRow tRow;

// Add some types

tRow = myNews.NewsType.NewNewsTypeRow();

tRow.type_id = 1;

tRow.name = "first type";

myNews.NewsType.Rows.Add(tRow);

tRow = myNews.NewsType.NewNewsTypeRow();

tRow.type_id = 2;

tRow.name = "second type";

myNews.NewsType.Rows.Add(tRow);


// Add some datat to the news

News.NewsTableRow nRow = myNews.NewsTable.NewNewsTableRow();


nRow.type_id = 1;

nRow.body = "some coppy here";

myNews.NewsTable.Rows.Add(nRow);


MessageBox.Show(nRow.body.ToString());


DataRow pRow = nRow.GetParentRow("NewsTypeNewsTable");

if (pRow == null)

{

MessageBox.Show("p is null");

}

else

{

MessageBox.Show(pRow[0].ToString());

}

DataRow[] pRows = nRow.GetChildRows("NewsTypeNewsTable");


MessageBox.Show(pRows.Length.ToString()); // RETURNS 0

I am trying to understand typed datasets more.... and i am stuck on a little problem

I have created a type dataset called "News" this consists of 2 tables. NewsTable and NewsTypes.
the news has news_id int, title string and type int
the news type table has type_id int and type_name string
There is also a relationship created which is joining on the news.type and then newstype.type_id

In the code below i am putting in 2 rows for types... and 1 row for a news article.

My question is .... how can i get the STRING value of the newstype that relates to my news item? eg i want an easy way to get to the value "first type"

Any help would me much appreciated.

News myNews = new News();
News.NewsTypesRow tRow;

// Add some types
tRow = myNews.NewsTypes.NewNewsTypesRow();
tRow.type_id = 1;
tRow.type_name = "first type";
New<type>Row() just creates a new row object of the type. It doesn't add the new row to the DataSet. Try

myNews.NewsTypes.Add(tRow).

Also, you need another new row here:

tRow = myNews.NewsTypes.NewNewsTypesRow();

tRow.type_id = 2;
tRow.type_name = "second type";
myNews.NewsTypes.Add(tRow)
// Add some datat to the news
News.NewsTableRow nRow = myNews.NewsTable.NewNewsTableRow();
nRow.title = "news title";
nRow.type = 1;
myNews.NewsTable.Add(nRow);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top