2

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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Learner
  • 1,286
  • 6
  • 34
  • 57

4 Answers4

5

You have invalid SQL - the aliases that contain spaces need to be escaped, as do aliases that are keywords.

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);
Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

You are using Date as alias (vchr_Date as Date). Date is a reserved keyword, try:

vchr_Date as [Date]

or change Date to some other alias.

You also need to use [] around aliases with space.

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
2

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

You have incorrectly specified column names . For instance, the fragment:

vchr_No as voucher No

should be expressed as

vchr_No as [voucher No]

or you will not have valid SQL.

In general, you need to put [ and ] around column names that contain spaces, punctuation, keywords, or any other characters that are not permitted in SQL column identifiers.

John Källén
  • 7,551
  • 31
  • 64