So I'm trying to run my code when i get this error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near 'b'.
The code looks like this:
string query = "select a.Navn from Ejer a" +
"INNER JOIN SlipsEjer b ON a.Id = b.EjerId" +
"Where b.SlipsId = SlipsId";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
command.Parameters.AddWithValue("@SlipsId", SlipsLB.SelectedValue);
DataTable ejerTable = new DataTable();
adapter.Fill(ejerTable);
EjerLB.DisplayMember = "Navn";
EjerLB.ValueMember = "Id";
EjerLB.DataSource = ejerTable;
}
I don't know why.. I've tried and run it through a new query in Sql and it works fine there..
Really hope u guys can help! Thank you
Add a space at the end of each line. Also add a @ symbol for the parameter.
string query = "select a.Navn from Ejer a " +
"INNER JOIN SlipsEjer b ON a.Id = b.EjerId " +
"Where b.SlipsId = @SlipsId";
Someone suggested using a verbatim string in the comment. Here's a sample of verbatim string. You can easily copy your query from Management Studio. Although I prefer using a stored procedure.
string query = @"
select a.Navn from Ejer a
INNER JOIN SlipsEjer b ON a.Id = b.EjerId
Where b.SlipsId = @SlipsId
";