Philip Rieck

Phil in .net

Strongly typed dataset tip

While doing code reviews (for .net code, of course!),  I see one minor annoyance with strongly typed datasets again and again - not using the TableName property.  I don't know why this comes up so often, so I thought I'd add a quick tip about it.

If you have a strongly typed dataset (some call them XSD datasets), great. You don't need to remember column names or worry about casting values.  So why do I see things like this:

dAdapter.TableMappings.Add("Table1", "MyTable");
//or
cntrl.DataMember("MyTable");

Instead of using the table's name as a string, use the .TableName property as such:

dAdapter.TableMappings.Add("Table1", typedSet.MyTable.TableName);
//or
cntrl.DataMember(typedSet.MyTable.TableName);

 

Now you can rename your table and let the compiler find all of the code you need to change, instead of trying to do a “Find” and hoping you didn't miss any.  I'm all about making the compiler find as many problems as I can.

[cross-posted from Philip Rocks, Philip Rieck's new-ish real blog - http://philiprieck.com/blog/archive/2004/01/21/152.aspx]


Comments

Richard M Mathis said:

Thats an awesome tip. I'm sure its going to save me a lot of headache, as I begin working with strongly typed datasets. Thanks!
# March 18, 2004 5:05 PM

Alejandro said:

is there a way to get the column names, the same way as with the column names?

I'd like to be able to use
typedSet.MyTable.MyColumn, and get "MyColumn". As it is now, we've hardcoded "MyColumn" all over the place....

Best regards/
Alejandro
# March 21, 2004 5:05 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)