Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
txtTotal.Text = getTotal("Select (sum(total_netpay),0) from emp_payroll where week(PDate) =week(now())").ToString("#,##0.00");

The problem is Incorrect syntax near ','.'

What I have tried:

C#
txtTotal.Text = getTotal("Select ifnull(sum(total_netpay),0) from emp_payroll where week(PDate) =week(now())").ToString("#,##0.00");
Posted
Updated 12-Jun-23 2:54am
v2
Comments
mtoha 12-Jun-23 2:22am    
what is get total function?
Hackweiser27 12-Jun-23 2:34am    
public double getTotal(string sql)
{
double _total = 0;

// try
{

P_Connection NewConnection = new P_Connection();
NewConnection.Connection_P();


SqlCommand comm = new SqlCommand(sql,P_Connection.conn);
// comm.Connection = P_Connection.conn;

_total = double.Parse(comm.ExecuteScalar().ToString());

}
// catch
{
MessageBox.Show("Error", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
return _total;

}
mtoha 12-Jun-23 2:36am    
when you debug string sql on get total, what is the value of sql string?
Hackweiser27 12-Jun-23 2:48am    
0
Hackweiser27 12-Jun-23 2:58am    
I tried to change ifnull to ISNULL my error is An expression of non-boolean type specified in a context where a condition is expected, near 'DATENAME'.'


Try to change your code like this :

txtTotal.Text = getTotal("Select ISNULL(sum(total_netpay),0) from emp_payroll where PDate is not null AND  DATENAME(week, PDate) = DATENAME(week, getdate()) AND year(getdate()) = year(PDate)").ToString("#,##0.00");
 
Share this answer
 
Comments
Hackweiser27 12-Jun-23 3:18am    
Thank you @mtoha. i got this error: Operand data type varchar(max) is invalid for sum operator.'
CHill60 12-Jun-23 4:10am    
Your database design has total_netpay stored in varchar(max) - don't do that. Always use the correct data type for the data you are storing - in this case one of the numeric types would be appropriate.
See What Are Data Types and Why Are They Important?[^]
mtoha 12-Jun-23 5:35am    
this style is called cowboy coding.. new style of coding anounced at 1900.
mtoha 12-Jun-23 3:22am    
change to this:
txtTotal.Text = getTotal("Select ISNULL(sum(convert(decimal, isnull(total_netpay, 0)),0) from emp_payroll where PDate is not null AND DATENAME(week, PDate) = DATENAME(week, getdate()) AND year(getdate()) = year(PDate)").ToString("#,##0.00");
Hackweiser27 12-Jun-23 6:01am    
OK THank you.
IFNULL is not an SQL Server function: it's only applicable to MySql and SqLite - for which you would need a different set of DB classes: MySqlConnection, SqliteConnection, ... and so on.

The SQL Server equivalent is ISNULL
 
Share this answer
 
Comments
Hackweiser27 12-Jun-23 3:11am    
yes, I found it my problem is when I try again my error is An expression of non-boolean type specified in a context where a condition is expected, near 'DATENAME'.'

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