Wednesday, March 7, 2012

Execute Scalar ?

I'm trying to do something like the code below, but it's saying "specified cast is not valid"

If i change the value returned to an "int", it works fine. My issue is, i'd like to get the value returned with more accuracy than an int as there will be 2 decimal places.

protectedfloat getProjectHours(string project)

{

string selectCmd ="SELECT SUM(hours) FROM tasks WHERE project=@.project";

string strConnection =ConfigurationManager.ConnectionStrings["TimeAccountingConnectionString"].ConnectionString;

SqlConnection myConnection =newSqlConnection(strConnection);

SqlCommand myCommand =newSqlCommand(selectCmd, myConnection);

myCommand.Parameters.Add(newSqlParameter("@.project",SqlDbType.VarChar));myCommand.Parameters["@.project"].Value = project;

myConnection.Open();

float total = (float)myCommand.ExecuteScalar();

myConnection.Close();

return total;

}

Could you post the sql function please?

|||

what do mean? it's all up there

|||

If this is the line you are getting an error on, then try this:

double total = Convert.ToDouble(myCommand.ExecuteScalar());

|||

worked perfect! Thanks!

|||

you are welcome...

|||

Oh, sorry. Need more coffee...

No comments:

Post a Comment