I have something like that:
Course
: CID - CNameDiscipline
: MID - MNamev - MProf
I have to create a list of disciplines, because one course will have more than one. So I followed this link and created a third table, just like that:
CREATE TABLE coursedisciplines (
courseID int references curso (CID),
disciplineID int references materias (MID),
constraint pkcoursedis primary key (courseID, disciplineID)
)
I'm able to add Data for the first table (course) and for the second (discipline). Sadly, when trying to connect them using the third, I get an error about the syntax. I looked for documentation but haven't found what I'm looking for. Here it's part if the code.
id = cmd.LastInsertedId;
try
{
cmd.CommandText = "INSERT INTO materias(MName, MProf) VALUES (@MName, @MProf)";
cmd.Parameters.AddWithValue("@MName", txtMName.Text);
cmd.Parameters.AddWithValue("@MProf", txtMProf.Text);
cmd.ExecuteNonQuery();
//The error is below
cmd.CommandText = "INSERT INTO coursedisciplines(courseID, disciplineID) VALUES(id, SELECT MID FROM discipline WHERE MName = '"+ txtMName.Text +"')";
cmd.ExecuteNonQuery();
}
Do you guys know what is wrong? Is this method really bad? I read a bit about serialize but don't want to go this way.
EDIT It worked here. Had to use select insert instead of select values. Thanks!