>I have the following code (a self join because I have employees and dependants in the same table):
>
>
> using (SqlCommand cmdGetBastaDependants = new SqlCommand("SELECT D.emp_number " +
> "FROM Employees as D " +
> "INNER JOIN Employees as E ON D.emp_empfk = E.emp_pk" +
> "WHERE E.emp_number = @emp_number AND D.emp_active = 1 ", dbConnection))
> using (SqlDataAdapter bastaDependantsAdapter = new SqlDataAdapter(cmdGetBastaDependants))
> {
> cmdGetBastaDependants.Parameters.AddWithValue("emp_number", tatilEmployeesRow["family_eligibility_ud"]);
> bastaDependants.Clear();
> bastaDependantsAdapter.Fill(bastaDependants);
>}
>
>
>when the last line runs I get an error:
>
>System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'E'.
>
>The SQL works fine if I run it in SSMS. Any ideas what is wrong with it in my code?
"INNER JOIN Employees as E ON D.emp_empfk = E.emp_pk" +
"WHERE E.emp_number = @emp_number AND D.emp_active = 1 ", dbConnection))
Try putting a space after the pk in the first line.
PS
C# allows this construct:
commandtext = @"select etc, etc. etc"
You can paste the query that works in SSMS right into the C# code without all the +, etc, almost like TEXT in VFP
Anyone who does not go overboard- deserves to.
Malcolm Forbes, Sr.