JoelDixon.com

"And so I ask you this one question. Have you ever tried simply turning off the TV, sitting down with your children, and hitting them?" - Bender, Futurama 4ACV06

Blog
General
Travel
Sport
Hockey
Technical
Gaming
Catalogue
Movies
TV Shows
Music
Video Games
Pages
Random Lists
Links
About


Blog Archive
Current Blogs
July 2010 (1)
June 2010 (2)
May 2010 (2)
April 2010 (3)
March 2010 (1)
February 2010 (3)
January 2010 (5)
2009 (28)
2008 (25)
2007 (67)
2006 (23)
2005 (32)
2004 (10)

Blog Tags
General
10 in '10 (4)
Books (1)
DVDs (2)
Funny (13)
Gadgets (5)
Hawt (1)
Movies (13)
Music (6)
Pets (4)
Recipe (16)
TV Shows (1)
Web (29)
Travel
Calgary (2)
Edmonton (12)
Kuala Lumpur (1)
New York (7)
Niagara Falls (2)
Phuket (2)
Pittsburgh (5)
Washington (4)
Sport
Basketball (1)
Running (3)
Hockey
Pittsburgh Penguins (25)
Technical
.NET (3)
Java (4)
Software (3)
Work (2)
Gaming
Commodore 64 (2)
Master System (1)
Mega Drive (1)
Xbox 360 (15)
XNA (1)

GamerTag


The rarely updated blog of Joel Dixon
< Vroom Vroom, sniff! | I've toured around the world, from London to the Bay... >

Thursday, April 07, 2005

ADO.NET Strongly Typed Relationships

# Posted by Joel Dixon at 07/04/2005 01:55:00
Updated by Joel Dixon at 21/09/2007 04:03:35 - added comment captcha


It's been over a month since my last blog - but that's OK - I'm sure no-one will mind!

I've decided to add a little technical advice about accessing data with Strongly Typed DataSets in ADO.NET. Reason being, I spent over an hour looking for this information before I finally tracked it down on someone's blog. After my post, it will double the amount of blogs with this information

In my ASP.NET WebService, I like to use strongly typed DataSets to access my data with ADO.NET, it's a whole lot cleaner that way. For my example, let's say we have a Customers table, and an Orders table. Customers are identified by their CustomerID, and orders by their OrderID (yes, I'm using Northwind). The Orders table also has the CustomerID that made the order (a one to many relationship). Defining the strongly typed DataSet for this in Visual Studio.NET is quite easy:

  1. Right-click your Project in the Solution Explorer and select Add > Add New Item...
  2. Select "Data Set" from the Templates list, and type the name for your item (CustomersDataSet.cs).
  3. In the Server Explorer, navigate to the Northwind database tables. Drag across the Customers and Orders tables.
  4. Select the Customers table and select Schema > Add > New Relation... from the File Menu.
  5. This is where your relationship is defined. Change the name to CustomersOrders, and change the Child element to Orders. Clicking OK will then create the relationship for you.

Now that you have the strongly typed DataSet, you can directly refer to column names. For example, instead of:

Code:

DataSet1.Tables["Customers"].Rows[0]

you can use:

Code:

DataSet1.Customers[0]

That was all good - but I wanted to look at the Orders that are assigned to a Customer (as per the relationship I added above). Most examples were telling me to do something like this:

Code:

CustomersDataSet.CustomerDataRow customer
= myDataSet.Customers.FindCustomerByID(1);
if (customer != null)
{
// Print out order numbers
foreach (DataRow orderRow in
customer.GetChildRows("CustomerOrders"))
{
Console.WriteLine(orderRow["OrderID"]);
}
}

This gets the job done - but we're back to using things like orderRow["OrderID"] instead of orderRow.OrderID. The disadvantage to using GetChildRows is that it returns an array of DataRow's - not Orders. A way to achieve the above code using your strongly typed relationship is as follows:

Code:

CustomersDataSet.CustomerDataRow customer
= myDataSet.Customers.FindCustomerByID(1);
if (customer != null)
{
// Print out order numbers
foreach (CustomersDataSet.OrderRow orderRow
in customer.GetOrdersRows())
{
Console.WriteLine(orderRow.OrderID);
}
}

When the relationship was created above, it added a GetOrdersRows() method on the CustomersRow object. It may not seem like much - but having the strongly typed OrderRow can help (well, it helped me).

After doing a quick search on GetOrdersRows() (to see if I had missed any examples of this) - I found a good ADO.NET tutorial. I wish I had have found that a few hours ago!


Back to Top

Comments

hey thanks for adding us to the links on your page!

# Posted - 20/04/2005 06:54:00
No worries - you know that that little link will probably bring a large percentage of my 3 readers to your site



# Posted - 20/04/2005 12:18:00
ha! you'd be surprised. perhaps only 3 people post comments but you can never really be sure how many people read.

# Posted - 21/04/2005 09:16:00
Greetings,

I am struggling with this in VS 2005. I decided to do as you did and google "GetOrdersRows()".

When I call GetOrdersRows() I get an invalid cast exception. Here is my code:

If I comment that out, it works fine.

I dragged Customers and Orders onto the XSD designer, but did not add the relationship, because it was already there.
I've tried it with the "Nested" checkbox either checked or unchecked (default), but no luck.

Do you have any advice?
Thanks!

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using CustomersDataSetTableAdapters;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CustomersDataSetTableAdapters.CustomersTableAdapter
cta = new CustomersTableAdapter();
CustomersDataSet.CustomersDataTable cdt =
cta.GetData();

StringBuilder sb = new StringBuilder();

cdt.WriteXml("c:\\temp\\output.xml", true);

foreach (CustomersDataSet.CustomersRow customer
in cdt)
{

sb.AppendFormat("Customer ID: {0}\n",
customer.CustomerID);

foreach (CustomersDataSet.OrdersRow
order in customer.GetOrdersRows())
{
sb.AppendFormat("Order: {0}\n",
order.OrderID + " " +
order.OrderDate);
}
}

lblOutput.Text = sb.ToString();

}
}


# Posted - 17/09/2005 00:37:00
PS: I get the same thing in VS.NET 2003 too.

# Posted - 17/09/2005 00:51:00

You must be logged in to comment
Blog Search

Advanced Search


Recent Blogs
Phuket, Thailand - 2010
Mr. Potato Head Mashups
Random Travel #2 - Thailand
You have been the ones, you have been the ones for me.
Balls
Caramelised Pumpkin Risotto
Malaysian Vegetable Curry
Kuala Lumpur, Malaysia - 2010
Yeah ... Baby!
xkcd Parody

Feed
Subscribe to feed Blog Entries

Add to Google
Add to My Yahoo!
Add to Netvibes

Recent Comments
Balls
posted 4 months ago by joeldixon
Exactly. What's wrong with the kids today when they can't spell genitals? And you and I have been using ... link

Balls
posted 4 months ago by abrereton
I'm just glad that they spelt it correctly in the end. link

Kuala Lumpur, Malaysia - 2010
posted 4 months ago by Aland84
Looks like you had a ball at KL. There are plenty of hotels in Kuala Lumpur in the $140 a ... link

Kuala Lumpur, Malaysia - 2010
posted 5 months ago by joeldixon
Definitely, you have to let us know when you're free for UBs! link

Kuala Lumpur, Malaysia - 2010
posted 5 months ago by Hitman
Awesome - Dixon, Awesome mate, living life to the max. You'll have to fill me in more next ... link


Comment Standings
1. the man with no name (45)
2. Hoff (39)
3. Gav (27)
4. Hitman (26)
5. Brad (22)
6. Eryc-Ads (13)
7. Deep Lurker (6)
8. thefury (5)
9. Dieter (4)
10. Zelks (4)

About This Site | Contact Me | 39 days until then