Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello all

I am facing an error message next to those sql statement , so can you please help me by writing it in a correct way that can be work! an errors message is: Incorrect syntax near the keyword 'AND'.

C#
@"SELECT JobNum, Dept, PubDate, EndDate, Employer,VacCountry, VacState, 
VacCity, Jobtitle, CompLogo, Wtag, SUBSTRING(jobdesc,1,40) as jobdesc FROM jobs WHERE VacCountry = @Location and CHARINDEX(@Wtags, Wtag)>0 ORDER BY PubDate DESC;


C#
string query = @"SELECT UID, Country, State, City, Wtag,Logo, Website, UsrType, BizCateg, BizSubCateg, Twitter, GooglePlus,Facebook, CompNme,SUBSTRING([CompDesc],1,40) as CompDesc From UserInfo where Country = @Location and UsrType = 'Business' and CHARINDEX(@Wtag, Wtag)>0 ORDER BY [RegDate] DESC";
Posted
Comments
PIEBALDconsult 25-Aug-15 17:36pm    
Aaaaand... what database system is this?
j snooze 25-Aug-15 17:39pm    
Should we assume you are filling in the @Location and @Wtag parameters correctly?
RossMW 25-Aug-15 18:19pm    
Have you tried removing parts of the SQL statement until it works and hence determining which part of the statement is causing the issue. The syntax looks OK for a SQL Server, assuming the fields are named correctly etc.
jgakenhe 25-Aug-15 22:18pm    
add a space before and after >
Sinisa Hajnal 26-Aug-15 2:35am    
Put those queries into your database query program (whichever that may be) and execute with some fixed values. Once you get it to work, you copy it into the code and replace the parameters. It would help, in this kind of questions, if you posted your calling code, maybe you're calling it wrong (so you get wrong result). Consider using SQL Profiler so you can see what you're sending to the database. If @Location is varchar and you're doing a replace, you're missing single quotes...maybe one of your values contains invalid characters...from what little you're showing its hard to be precise.

1 solution

First try to debug your query separately in your sql server (If sql server is your query program). And try to pass direct value to @location and @Wtag variables.
1. If you are able to run query successfully then chances are you are passing wrong parameters from your page as dynamic query.

2.If you are not able to run query from your query program then try to run it without WHERE clause and one by one add your clauses upto which you can run your query successfully.

There is nothing wrong with the syntax your writing. something wrong with your variable assignment.
 
Share this answer
 

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