I have a bug in this method. "INSERT INTO syntax error"
I don't know how to fix it. help me please
public void Add(string MName, string Color, int Cost)
{
connection.Open();
command = new OleDbCommand("INSERT INTO Catalog (MName, Color, Cost) VALUES (@MName, @Color, @Cost)", connection);
command.Parameters.AddWithValue("@MName", MName);
command.Parameters.AddWithValue("@Color", Color);
command.Parameters.AddWithValue("@Cost", Cost);
command.ExecuteNonQuery();
connection.Close();
}

Mahesh ChandRank #1 · 393.3K reputationPosted Dec 5, 2023, 11:45 AM EST
You are passing variables within the double quote. That needs to come out of the " ".
Something like this:
command = new OleDbCommand("INSERT INTO Catalog (MName, Color, Cost) VALUES ("+@MName+","+ @Color + "," + @Cost +")", connection);
I just typed .. you may have to fix the exact syntax. You can also use another string concatenation method. Here is an article on this:
https://www.c-sharpcorner.com/article/6-effective-ways-to-concatenate-strings-in-c-sharp-and-net-core/
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 6, 2023, 1:03 AM EST
please use potential paramter in your insert statement rather then named parameter,
public void Add(string MName, string Color, int Cost) { connection.Open(); command = new OleDbCommand("INSERT INTO Catalog (MName, Color, Cost) VALUES (?, ?, ?)", connection); command.Parameters.AddWithValue("@MName", MName); command.Parameters.AddWithValue("@Color", Color); command.Parameters.AddWithValue("@Cost", Cost); command.ExecuteNonQuery(); connection.Close(); }