DKR DKR - 28 days ago 16
C# Question

Error while executing sql join query from c# ADO.net

I am trying to execute a simple SQL query from c# using ADO.net adapter.

Query access data from two tables using join.

Below is my code

newRequest=getrequest.selectQuery("select A.associateID, A.name, A.role, A.Salt FROM associate A INNER JOIN UserStatus UA ON UA.statusID=A.status" +
"WHERE A.coordinatorID='"+cID+"' AND UA.statusName='WAITING FOR APPROVAL'");

con.Open();
SqlDataAdapter sd = new SqlDataAdapter(query, con);
sd.Fill(ds);


I get error on execution:

Incorrect syntax near 'A'

Answer

Try below code:

newRequest=getrequest.selectQuery("select A.associateID, A.name, A.role, A.Salt FROM associate A " +
"INNER JOIN UserStatus UA ON UA.statusID=A.status "
"WHERE A.coordinatorID='" + cID + "' "
"AND UA.statusName='WAITING FOR APPROVAL'");

con.Open();               
SqlDataAdapter sd = new SqlDataAdapter(query, con);
sd.Fill(ds);
Comments