Back Office
Back Office

Reputation: 101

Unable to cast object of type System.Int64 to type System.Int32

I get Unable to cast object of type 'System.Int64' to type 'System.Int32' on:

item.ItemCount = reader.GetInt32(reader.GetOrdinal("amount"));

I tried:

item.ItemCount = reader.GetInt64(reader.GetOrdinal("amount"));

But I got:

Error   CS0266  
Cannot implicitly convert type 'long' to 'int'. 
An explicit conversion exists (are you missing a cast?)

The field is bigint and this is my first experience with .Net.

Upvotes: 3

Views: 11248

Answers (1)

User2585
User2585

Reputation: 372

You have to use GetInt64

item.ItemCount = reader.GetInt64(reader.GetOrdinal("amount"));

SQL bigint is the equivalent to .NET long, GetInt64 returns a long, while GetInt32 returns an int.

See this documentation for more detail.

Upvotes: 5

Related Questions