Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Look Ma! A datatable field reference with no quotation marks!

Sexy Sahil said bad, bad things about ADO.NET 2.0's Strongly Typed DataSets.  He's an MVP and he made good points.  Me?  I'm just a simple developer who is always grateful when I don't have to think much.  And one of those nasty ADO.NET 1.0 features requiring me to think more than I had to was having to write

dt.Rows[0]["SomeHardToRememberFieldName"]

Not only did I have to type out the whole darn field name, I had to pop open the Server Window to display the table/view fields.  Now, thanks to the new DataTableAdapter and other ADO.NET 2.0 Strongly Typed sweetness, I don't have to.  Check out these two examples of populating a TreeView Control in ADO.NET 1.0 and 2.0.  First the ADO.NET 1.0 way:

foreach (DataRow row in m_subject.Staffers.Rows)
{
 TreeNode stafferNode = new TreeNode((string)row["DisplayName"]);
 stafferNode.Tag = (int)row["StafferID"];

 int icon = (int)GetIconType((string)row["StafferType"]);

 stafferNode.ImageIndex = icon;
 stafferNode.SelectedImageIndex = icon;
 staffersNode.Nodes.Add(stafferNode);
}

Now for the ADO.NET 2.0 version:

foreach (MyDataSet.dtUsersRow rw in myDataSet.dtUsers.Rows)
{
 TreeNode stafferNode = new TreeNode(rw.DisplayName);
 stafferNode.Tag = rw.StafferID;

 int icon = (int)GetIconType(rw.StafferType);

 stafferNode.ImageIndex = icon;
 stafferNode.SelectedImageIndex = icon;
 staffersNode.Nodes.Add(stafferNode);
}

THAT oughta help you Put On a Happy Face!  (If it doesn't and you're still blue, think of Janet Leigh as she looked in Bye-Bye Birdie.)

 

Comments (7) | Post RSS RSS comment feed

Posted on 6/25/2005 2:13:00 PM by Dave Burke
Categories: .NET
Tags:

Related posts

Comments (7) -

6/25/2005 6:10:49 PM Permalink

Maybe I should make a few comments on how .NET 2.0 strongly typed datasets are good Smile.

Lets just say, there is more good, less bad.

Sahil Malik |

6/25/2005 6:14:58 PM Permalink

Sahil, If you did post on them being good, I missed it.  Hey, you don't have to post on strongly typed datasets being good.  It's not like you're writing a book on ADO.NET 2.0 or anything. Smile

daveburke |

6/27/2005 7:37:55 AM Permalink

The crappy support for such things in previous versions is one of the things that's given me a bias for business objects.  When I was playing with the .net 2.0 beta I was very glad to see they had improved the coding experience we have with DataSet's.

Steve |

6/27/2005 7:38:09 AM Permalink

I'm confused.  In ADO.NET 1.0, wouldn't a typed dataset do exactly the same thing as you're demoing for 2.0?

Oskar Austegard |

6/27/2005 7:43:55 AM Permalink

Oskar, Good point.  Strongly typed datasets in 1.0, yes, though I rarely used them.  Maybe that's why I'm not very clear.  The new DataTableAdapter provides more intelligence/intellisense at the datatable level.  

All I can say for sure at this early phase is that IssueVision and my 2.0 app use the very same DataSet approach, and I have intellisense for fields where in the IV app they did not.  I'm still digging into ADO.NET 2.0 features.  Thanks for asking the question.

daveburke |

6/28/2005 9:13:15 PM Permalink

To get a feel for some of the new features, including many of the changes we made between B1 and B2, checkout our team blog.  You'll wan to navigate to the older blog for the specifics.  Yes, the strong typing Dave mentions in his treeview example was available in 1.0.  The big difference is with TableAdapters you get the same strong typing around parameters and queries against your database.  Instead of having to say:
Me.SqlDataAdapter1.SelectCommand.Parameters(0).Value = 1
You can say
Me.CustomersTableAdapter.GetByCustomerId(1)
You no longer have to guess which index, or name of the parameter.  You don't have to guess what data type the parameter is.  And with the GetBy methods, you don't even need to create the whole dataaset just to get a single table.  To get a feel of this, add a few tables through the Data Sources Window.  Open the DataSet Designer and right click on the TableAdapter.  Select Add Query and add a query which returns the same schema, but has a where clause.  Name the queries, then drag the table from the data sources window to your form.  
Right click on the TableAdapter in the component tray and select Add New Query.  Select one of the existing queries you just created and click finish.  VS will create a toolstrip with a textbox for each parameter.
Notice the properties and methods on the TableAdapter...
Anyway, that should get you started,
Enjoy
Steve

PS, yes, good feedback is always welcome.  It really helps reward the team as they've been heads down for a few years working on all the new, cool features.  Constructive feedback is also really important as we don't know what we don't know.  While we do try to troll the blogs, the most effective way to enter feedback is through ladybug: http://lab.msdn.microsoft.com/productfeedback/
While we do have people scrubbing and verifying the bugs, they do get to the product teams.  Don't be afraid to send us a note on our team blog as well.  But hurry, we're really closing down so we can ship and get on with the next release...

Steve Lasker |

6/29/2005 5:02:04 AM Permalink

Steve, ADO.NET 2.0 is a surprise winner of Cool in my experience with VS2005 so far.  I can't think of a thing in 1.0 I didn't like that wasn't addressed in 2.0.  I'll definitely be blogging more about my experiences.  

Thanks for explaining the extension to strong-typing.  That's definitely one of my favorite new features.

daveburke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke