There are a couple of problems with the example on this page. The first one isn't really a code bug, but a logic bug:

Code:

while (rdr.Read())
{
goalsFor += Convert.ToInt32(rdr["GoalsFor"]);
goalsAgainst += Convert.ToInt32(rdr["GoalsAgainst"]);
if (goalsFor > goalsAgainst)
wins++;
else if (goalsFor < goalsAgainst)
losses++;
else
draws++;
}

Checking for wins and losses this way doesn't make any sense, because the code is evaluating the

*cumulative* goal totals. It should be comparing the goalsFor and goalsAgainst for each individual game:

Code:

while (rdr.Read())
{
int thisGameGoalsFor = Convert.ToInt32(rdr["GoalsFor"]);
int thisGameGoalsAgainst = Convert.ToInt32(rdr["GoalsAgainst"]);
if (thisGameGoalsFor > thisGameGoalsAgainst)
wins++;
else if (thisGameGoalsFor < thisGameGoalsAgainst)
losses++;
else
draws++;
goalsFor += thisGameGoalsFor;
goalsAgainst += thisGameGoalsAgainst;
}

The second problem is this line:

Code:

winRatio = Convert.ToInt32(wins / losses * 10);

Because the wins and losses variables are integers, this expression will always evaluate to zero if there are more losses than wins. In order for this work, you have to do a conversion to a floating point number. There's probably a more elegant way to do this, but here's what I did to get it to work:

Code:

double winPercent = wins * 1.0 / losses;
winRatio = Convert.ToInt32(winPercent * 10);

Finally, there's a problem with the downloaded code in Begin\Chapter09. If you look at the data in the Fixtures table in WroxUnited.mdf, you'll see that there are NULL values in the GoalsFor and GoalsAgainst in the tenth record. You'll have to either delete this record or enter numbers for these fields - otherwise, you'll get a runtime error.