I'm currently trying my hand at constructing an online game. In my
system, the attacks a character can execute are themselves objects,
and their info is stored within the db. Unfortunately, I'm having a
problem retrieving attack info from the db. Specifically, I'm getting
an InvalidCastException when trying to retrieve an integer value.
My table is pretty straight forward:
hackerAttacksId - int identity primary key
level - int
name - string
tpCost - int
dmgModifier - float
hitPenalty - float
My code, which is in the constructor for my HackerAttacks registry
object:
public class HackerAttacks : Attacks
{
public HackerAttacks()
{
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["GitSConnectionString1"].ConnectionString);
SqlCommand cmd;
SqlDataReader reader;
string query = "Select * from DBHackerAttacks";
cmd = new SqlCommand(query, conn);
conn.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
HackerAttack hackerAttack = new
HackerAttack(reader.GetInt32(1), reader.GetString(2),
reader.GetInt32(3), reader.GetFloat(4), reader.GetFloat(5));
this.attacks.Add(hackerAttack.Level, hackerAttack);
}
}
/* ... */
}
The problem occurs with the 'reader.GetInt32(3)' argument. I'm not
sure why there's an InvalidCastException being thrown there, as column
3 in the database is the tpCost column, which is simply an int.
My db, at the moment, only has one row of data. The data leads me to
believe that the GetInt32 method has an issue with retrieving a 0 as a
value. I say this because I use the same method on column 1 (level),
which has a value of 1, and no exception is thrown.
Am I missing something obvious here?