specified cast is not valid error in executescalar.

  • Thread starter Thread starter trialproduct2004
  • Start date Start date
T

trialproduct2004

hi all
i am having application which is connecting to databset and executing
one query as below:-

string str;
str="select count(*) from table1;

and then i am using sqlcommand to get result as below:-

long l=(long)sqlcommand.ExecuteScalar();

at this line my code is giving error 'specified cast is not a valid'.

but if i am collecting result of executescalar in string then it is
working properly.

can some one tell me why this is happending.

Any help will be appreciated

thanks in advance.
 
Hi,

The return of ExecuteScalar is object , you are casting it to a long and
getting an exception , the reason:
the value you are getting is not castable to long

Solution:
use Convert.ToLong ( )

IIRC you are getting a SqInt32 struct which is castable to Int32

cheers,
 
Back
Top