c# - Getting an sqlException when I run ExecuteScalar -


i made method scalar database using executescalar throwing exception. sql select statement looks correct me. can me please.

the exact error i'm getting

incorrect syntax near 'invoices'.

code:

public static string gettotalbalancedue()  {         decimal totalbalancedue;          string selectstatement =             "select sum(invoicetotal - paymanttotal - credittotal) " +             "as balancedue invoices" +             "where invoicetotal - paymanttotal - credittotal > 0";          try         {             using (sqlconnection connection = payablesdbconnection.getconnection())             {                 connection.open();                  using (sqlcommand selectcommand = new sqlcommand(selectstatement, connection))                 {                     totalbalancedue = (decimal)selectcommand.executescalar();                 }             }         }         catch (sqlexception ex)         {             //exceptions thrown controller, view             //please make sure not use messagebox.show(ex.message) in dal             //because couples dal view              //throw used instead of throw ex because former preserves stack trace             throw;         }         catch (exception ex)         {             throw;         }          return system.convert.tostring(totalbalancedue);     } 

i modified select statement be:

            string selectstatement =                 "select sum(invoicetotal - paymenttotal - credittotal)" +                 "as balancedue" +                 "from invoices " +                 " vendorid =" + vendorid; 

but i'm still getting

incorrect syntax near 'invoices'.

modify

 string selectstatement =             "select sum(invoicetotal - paymanttotal - credittotal) " +             "as balancedue invoices" +             "where invoicetotal - paymanttotal - credittotal > 0"; 

to:

string selectstatement =             "   select sum(invoicetotal - paymanttotal - credittotal) " +             "   balancedue invoices" +             "   invoicetotal - paymanttotal - credittotal > 0"; 

Comments