Follow @RoyOsherove on Twitter

Nasty bug in Data Access Block Ver. 2.0

Thanks to Mark Brown for discovering this bug in the Data Access Block Ver. 2.0.

Short answer:

Here's how to fix the data access bug:

  • Open the SQLHelper.cs file
  • look for the line that contains this line of code:

C#:  tableName += (index + 1).ToString();

VB:  tableName = tableName & (index + 1).ToString()

  • Replace it with this line of code:

C#:  tableName= "Table" + (index + 1).ToString();

VB: tableName = "Table"  & (index + 1).ToString()

  • That's it.

 

Long answer

The bug is introduced when you try to use the SQLHelper.FillDataSet() method. basically the helper allows you to send in many SELECT statements, and pass in the names of the tables that the SELECTs will fill.

The table names are used to create TableMappings between the table names you provide and the tables that the SELECT statements will create in the dataset. A very nifty idea, only the idea is killed right at the third table because down under, the helper loops through the table names and maps each table to a corresponding default table name with a sequential number, so if I send "MyFirstTable","MYSecondTable","MyThirdTable" in the table names string array, they should be mapped to "Table","Table1","Table2" respectively.

sadly, what really happens is that the third table will be mapped to a table named "Table12" instead of "Table3". And it's all because of this line:

C#:  tableName += (index + 1).ToString();

VB:  tableName = tableName & (index + 1).ToString()

After two loops, tablename will already be "Table1", so anything you concatenate to it will just mess everything up. that's why changing it to this line will work every time:

C#:  tableName= "Table" + (index + 1).ToString();

VB: tableName = "Table"  & (index + 1).ToString()

Framework Compatibility Graphics initiative

DataReader.HasRows