Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error CS0266: Cannot implicitly convert type 'object' to 'int'

Tags:

c#

casting

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

int dd= 6000;
sqlCmdDefaultTime = new SqlCommand("myQuery", sqlCon);
sqlDefaultTime = sqlCmdDefaultTime.ExecuteReader();
while (sqlDefaultTime.Read())
{
      dd= sqlDefaultTime[1];
}

how can i cast

like image 679
Br3x Avatar asked Jan 17 '12 12:01

Br3x


2 Answers

Simple cast to int:

dd = (int)sqlDefaultTime[1];
like image 67
ken2k Avatar answered Nov 08 '22 04:11

ken2k


Try this...

int.TryParse(sqlDefaultTime[1].ToString(), out dd);

in the event that the parse is successful dd will now be a new value.

Unless of course the object is an int already, the you can just cast it...

dd = (int)sqlDefaultTime[1];
like image 26
musefan Avatar answered Nov 08 '22 04:11

musefan