Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Data Access Application Block, Part 2 (Halfa bug)

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.

 

Posted: Jul 30 2003, 04:28 PM by daveburke | with 3 comment(s)
Filed under:

Comments

Roy Osherove said:

I don't understand why this has not worked for you. The tables names are generated only to be used later inside the TableMappings colection of the dataset.
I'd love to see the code after you applied the 'fix' that did not work for you. perhpas something was missed? Also, I'd want to see the code that calls the FillDataset Method and failes.
send it over to royo@iserializable.com
# July 30, 2003 4:43 PM

Roy Osherove said:

PS:
Execuse the typos. it's almost midnight:)
# July 30, 2003 4:53 PM

TrackBack said:

# July 30, 2003 10:06 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)