Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sql

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.

like image 621
Back Office Avatar asked Jan 18 '26 02:01

Back Office


1 Answers

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.

like image 171
User2585 Avatar answered Jan 20 '26 15:01

User2585



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!