I have a little problem to insert from 2 different table’s in C# to MySql database
Here is my code where the error occurs
command.CommandText =
@"INSERT INTO personen (Name, Surname, GenderID, BirthplaceID)
VALUE @Name, @Surname, (SELECT GenderID FROM gender WHERE Gender = @GenderID),
(SELECT BirthplaceID FROM place WHERE placename = @BirthplaceID);";
When I replace the “@” statements and just fill in the name’s, it works properly.
So I want to know where the problem is.
Also the problem ain’t the parameters like some answers said, cause i’ve added them(see below).
insertname = data.Name;
insertsurname = data.Surname;
insertgenderid= data.Gender;
insertbirthplaceid= data.Birthplace;
command.Parameters.AddWithValue("@Name", insertname);
command.Parameters.AddWithValue("@Surname", insertsurname);
command.Parameters.AddWithValue("@GenderID", insertgenderid);
command.Parameters.AddWithValue("@BirthplaceID", insertbirthplaceid);
command.ExecuteNonQuery();
If the problem is what you list as the title:
Here is the sqlfiddle of this validating now (tables are all int just because syntax is what we are going for not, types)
First, it should be VALUES not VALUES. And second, the VALUES must be wrapped in parentheses.