I always enjoy Roy Osherove's writings on ADO.NET and other .NET subjects and so I studied his Filling Typed datasets posts which included a reported DAB bug, originally reported by Mark Brown on the dotnetjunkies blog site.
The bug, as excellently described by Mark in his blog post had to do with the SqlHelper.FillDataset() method, which creates a dataset with multiple tables. I applied "the fix" but still received
<ds>
<correcttable0name></correcttable0name>
<table1></table1>
<table2></table2>
</ds>
instead of
<ds>
<correcttable0name></correcttable0name>
<correcttable1name></correcttable1name>
<correcttable2name></correcttable2name>
</ds>
when using the sweet one-pass code found on Roy's post provided by Lior Rozner:
public ApartmentDataset GetDataset()
{
ApartmentDataset ds = new ApartmentDataset();
SqlHelper.FillDataset(CON_STRING,CommandType.Text,
"select * from Apartments " +
"select * from Cities " +
"select * from Statuses ",
ds,
new string[]{"Apartments",
"Cities",
"Statuses"});
return ds;
}
So I more closely examined the fix, and it didn't make sense, that is, the tables beyond the 0-index string array passed to SqlHelper.FillDataset were still going to be named Table+index# and that the fix did nothing to correctly name the tables (unless I'm missing something, which is always a possibility...) I tried several alterations to the fix until I realized that the sure way to name my dataset tables correctly was to iterate through and rename them after the dataset was populated in the FillDataset method a couple of lines below (@ 1847):
dataAdapter.Fill(dataSet);
for (int index=0; index < tableNames.Length; index++)
{
dataSet.Tables[index].TableName = tableNames[index].ToString();
}
That did the trick. Now when I use the one-pass approach I can use ds.tables["tablename"].whatever instead of ds.tables[2].whatever.
In review, if you want to use a single pass approach and have multiple tables correctly named, leave the code in SQLHelper.cs as it is and add the for loop after the da.Fill(ds) statement.