Dheeraj Kumar
Dheeraj Kumar

Reputation: 4175

Specified cast is not valid - bigint to long - C#

I am trying to map bigint data from sq server table to c# long variable. Which I believe is the correct way to map.

long id = (long)ds.Tables[2].Rows[0].ItemArray[0];

I also tried below as suggested on SO.

long id = (long)(double)ds.Tables[2].Rows[0].ItemArray[0];

With both above I get below error:

System.InvalidCaseException : Specified cast is not valid.

In case you are wondering what data it contains, it is "1".

Upvotes: 0

Views: 1464

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127573

Convert.toInt64 got it worked. Thanks all

You are just hiding a bigger problem you have. The datatype in your column of your c# DataTable object held within the DataSet ds is likely set incorrectly. You need to set your table up so that the column's property for DataType is set to be Int64.

Upvotes: 5

Related Questions