I'm a beginner with C#, when I execute the code, this error message occurs:
"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'"
My 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;
}
}
My database has three tables:
Recipe
(Id, Name, PrepTime, Instructions) Ingredients
(Id, Name) RecipeIngredient
(Id, RecipeID, IngredientID) a space between some words is missing! put a space " " first of each string constants. your string query is like this now:
"select a.Name from Ingredients ainner join RecipeIngredient b ON a.Id = b.IngredientID" +
and there is not any spaces between a
and inner
words.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.