Ich bin ein Anfänger mit C#, wenn ich den Code ausführen, diese Fehlermeldung auftritt:Ausnahme: ‚System.Data.SqlClient.SqlException‘ aufgetreten in System.Data.dll wurde aber nicht in Benutzercode behandelt
"Eine Ausnahme vom Typ 'System.Data.SqlClient.SqlException' aufgetreten in System.Data.dll wurde aber in Benutzercode
Zusätzliche Informationen nicht behandelt: falsche Syntax in der Nähe von 'b'"
Mein Code:
public void populateIngredients()
{
string query = "select a.Name from Ingredients a" +
"inner join RecipeIngredient b ON a.Id = b.IngredientID" +
"where b.RecipeID = @RecipeID";
using (con = new SqlConnection(connection))
using (SqlCommand cmd = new SqlCommand(query,con))
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@RecipeID", listRecipes.SelectedValue);
DataTable IngredientTable = new DataTable();
sda.Fill(IngredientTable); // The error points here
listIngredients.DisplayMember = "Name";
listIngredients.ValueMember = "Id";
listIngredients.DataSource = IngredientTable;
}
}
Meine Datenbank hat drei Tabellen:
Recipe
(ID, Name, prepTime, Anleitung)Ingredients
(ID, Name)RecipeIngredient
(Id, RecipeID, IngredientID)
Es fehlt ein Leerzeichen zwischen _from Zutaten a_ und das folgende _inner verbinden sich gleich vor dem _where_ – Steve