asp.net - Specified cast is not valid error using C# -
i trying store select statement result in c# variable getting error
"specified cast not valid"
when run it. not sure how resolve please help
sqlconnection con1 = new sqlconnection(configurationmanager.connectionstrings["myconnectionstring"].connectionstring); sqlcommand cmd1 = new sqlcommand("select cast(round(sum(cast(avgnumbers numeric(12,2))),2) decimal(12,2)) [average days] maintable ", con1); cmd1.connection = con1; con1.open(); int32 result = (int32)cmd1.executescalar(); con1.close();
cmd1.executescalar()
not returning boxed integer. assign object , @ in debugger see is.
i'm guessing it's going returning decimal or double, , need do:
int32 result = (int32)(double)cmd1.executescalar();
or:
int32 result = (int32)(decimal)cmd1.executescalar();
[edit in response question in commments below]
to maintain decimal value, this:
decimal result = (decimal) cmd1.executescalar();
if needed to, cast decimal double:
double result = (double)(decimal) cmd1.exectutescalar();
c# asp.net sql-server-2008
No comments:
Post a Comment