I am trying to insert some values into a database, using insert statement. I have to use select statement as well to get from another table the key that corresponds to the option selected.
I tried several queries but none of them worked.
string query3 = "insert into students (FirstName, LastName, FatherName,
Email, DateBirth, DateReg, Adress, Gender, Specialization, Country,
Province, City) values ('"
+ this.txt_fname.Text + "','" + this.txt_lname.Text + "','"
+ this.txt_fathername.Text + "','" + this.txt_email.Text + "','"
+ this.date_birth.Text + "', '" + this.date_reg.Text + "','"
+ this.txt_adress.Text + "','" + this.Gender
+ "', (select specialization_id from specialization where SpecializationName = '" + this.specialization.Text
+ "'),
(select country_id from country where CountryName ='" + this.comboBox2.Text
+ "'),(select province_id from province where ProvinceName ='"
+ this.comboBox4.Text
+ "'),(select city_id from city where CityName ='"+ this.comboBox3.Text + "');";
I expect the output "saved" but I get {"Incorrect syntax near ';'."}
When I use:
'" + ("SELECT specialization_id from specialization where SpecializationName =" + this.specialization.Text)+ "'
instead of (wrote above):
(select specialization_id from specialization where SpecializationName = '" + this.specialization.Text + "')
I get:
{"Conversion failed when converting the varchar value 'SELECT specialization_id from specialization where SpecializationName =Informatica Economica' to data type int."}
query3variable, try them in SSMS and post them here properly formatted. It's impossible to find out what that query does otherwise. Using parameters instead of concatenation would result in a far cleaner query too