DAL – SqlHelper.FillDataset() bug

Those of you using the Microsoft.ApplicationBlocks classes for your DAL might already have noticed (or not! which is the point of blogging about this) that the SqlHelper.FillDataset() method has many overloads some of which take a string array, supposedly to be used to automatically name the DataTables on the dataset for multiple recordsets returned.

While this seems handy in theory, it doesn’t actually work cuz the method’s got a bug. No matter how many strings you put into the array, the method will only name the first two tables accoding to what you have entered. After that it will name them as “Table” plus their index on the tables collection.

so, if you have 4 resultsets being returned and you pass in the following array for the tablenames arguments:

new string[] tablenames { “Red”, “Green”, “Blue”, “Yellow”, “Black” }

the tables on the dataset will actually be named : “Red”, “Green”, “Table2”, “Table3” and “Table4”

To get around this, I reccomend looping through the dataset right after the fill so everything is still isolated on the DAL, after all, this is the DAL’s job and it should’ve been done in first place anyway if it wasn’t for Microsoft releasing it with a bug.

so what I do first is set up an array with all the table names . Then right after the fill loop through the tables collection and set every table to their right name.

C#
string[] tablenames { “Red”, “Green”, “Blue”, “Yellow”, “Black” };
for (int i=0;i < dt.Tables.Count; i++) { dt.Tables[i] = tablenames[i]; } 

You might then wonder what to do with the tablenames argument in FillDataset(). Well, you can do whatever you want since it won’t matter, however, just for clarity, I like to make it as if it actually work and properly pass in the same tablenames colletion I use to rename the tables and add a comment flagging the bug.