Work with strongly typed DataSets and DataTables

I think every developer working with any programming languages and frameworks knows the benefits of strongly typed classes. This is particularly true in the .NET Framework, where string literals fall outside the scope of compiler checks, and given this, it’s easy to introduce errors which show up run-time only.

When you are working with DataSets (and the classes of System.Data), it is much more easier to generate these types of errors, because of the massive number of indexers used in this part of the framework. Think about how you’d retrieve DataTables from a DataSet, or DataColumns from a DataTable. Something like this:

DataTable dt = DataSet.Tables[“myTable”];
DataColumn dc = dt.Columns[“myColumn”];

Two possible places where typos can raise hell. Even worse, when dealing with untyped DataSets, casting is heavily used:

DataRow dr = dt.NewRow();
dr[“ProductID”] = 1;
int ProductID = (int)dr[“ProductID”];

Fortunately, there is a solution. However, this is not part of the .NET Framework, and has nothing to do with the System.Data namespace. It is a feature of Visual Studio, and it’s been around since version 7. It is called a strongly-typed DataSet. The main benefits are:

  • Type safety: no casting, and you can use your custom types safely.
  • IntelliSense support: no more typos in strings, and faster lookups, since columns are properties of rows, etc.
  • Wizards: Visual Studio provides powerful wizards to generate your strongly-typed DataSet in seconds.
  • Separation: Visual Studio watches out not to hard-code your connection strings. Instead, it creates a custom entry for the generated connection strings in the connectionStrings section of your .config file.

For those who like to write their codes by hand, I have bad news. Since Visual Studio’s DataSet Designer is a drag-and-drop interface, which autogenerates code, it’s not recommended (however, it’s possible) to edit the generated code by hand, because even minor changes regenerate the code, and your modifications will be lost.

After you’ve generated your DataSet with DataSet Designer, you can work with it in your code. Visual Studio generates a namespace for your newly created DataSet (usually DataSetName) which you can use to work with your data, in a strongly-typed manner. Since I work with my own production data source, and am a little bit lazy to write the Northwind counterpart of my projects, I won’t publish source codes here. Mostly, because type names are generated after the metadata found in the database, and it would be a little confusing to start writing about CustomersRow, CustomersDataTable, etc.

As it is really simple to create typed DataSets, simply dragging and dropping tables from Server Explorer, or using the TableAdapter wizards, I think you should experiment, instead of trying to reproduce my code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s