You have told us the .NET framework datatypes of your SQL Server
table. This information doesn't really help to identify the actual
SqlType. SQL Server 2005 has 4 types of ints which map to .NET
framework datatypes in the following way :
a) TinyInt - Maps to .NET Byte type.
b) SmallInt - Maps to .NET Int16 type.
c) Int - Maps to .NET Int32 type.
d) BigInt - Maps to .NET Int64 type.
Which of these is the tpCost column ? You would need to use the
appropriate DataReader.Get[] method.
On Nov 11, 6:56 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> 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?