Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I wanna to sum table field using ExecuteScalar() function in C#.net


}
return count_para;
}

but don't return anything , and i have following error :
Specified cast is not valid.

thanks a lot
Posted
Updated 25-Mar-15 1:01am
v6

1 solution

In order to use a parameter within your command you need to include the where clause:

C#
public string Calcu_soud(string stat_pay , string az_ti , string ta_ti)
{
    string gt = string.Empty;
    string _comm_calcu_soud = " SELECT SUM(kala.soud_mounth) AS Expr1 FROM (kala)";
    string _comm_calcu_soud += " WHERE status_pay = ?";
    OleDbParameter[] gh = new OleDbParameter[1];
    gh[0] = new OleDbParameter("status_pay", OleDbType.Char);
    gh[0].Value = Convert.ToString(stat_pay);
    gt =  a.Query_Select_Executescaler_with_para(_comm_calcu_soud, gh).ToString();
    return gt;
}


OleDbParamaters will have to be added in order of the qMarks ('?') as they don't use named parameters. In fact, you can name your parameter foo or bar and it won't effect the command.

Here are a couple of articles that show the parameters being used:
Intermediate OLE DB Consumer with C#[^]
And the difference between OleDb and Ado style parameters:
Parameters - SqlCommand vs. OledbCommand and OdbcCommand[^]

Hope that helps ^_^

Don't forget to like and subscribe :Þ
 
Share this answer
 
Comments
bernova 25-Mar-15 5:49am    
Thanks but just i have following error :
Specified cast is not valid.
Andy Lanng 25-Mar-15 5:59am    
I suspect that it is due to your datatypes in the db. What datatype is status_pay please
Andy Lanng 25-Mar-15 5:58am    
where does the error occur? could you post the full message please
bernova 25-Mar-15 6:03am    
datatype is string
and error is
Specified cast is not valid.
Andy Lanng 25-Mar-15 6:14am    
ok. what about kala.soud_mounth? is that a numeric datatype?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900