Search code examples
c#aliassqlexception

SQL exception while selecting the columns with alias names


My SQL query is

cmd = new OleDbCommand("select vchr_No as voucher No, vchr_Date as Date, 
                        vchr_Acnthd as Debit, vchr_Prtynm as Paid to 
                        from cshvchrs  
                        where vchr_No like '%" + vchno + "%' ", con);

When I try to retrieve the data I am getting an exception:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

Please anybody help me


Solution

  • On vchr_Date as Date part,

    Date is a reserved keyword on Transact-SQL.

    Reserved keywords are part of the grammar of the Transact-SQL language that is used by SQL Server to parse and understand Transact-SQL statements and batches.

    You can use it with square brackets [] like;

    vchr_Date as [Date]
    

    Use your full query as;

    cmd = new OleDbCommand("select vchr_No as [voucher No] ,vchr_Date as [Date] ,vchr_Acnthd as Debit ,vchr_Prtynm as Paid to from cshvchrs  where vchr_No like '%" + vchno + "%' ", con);