I have problem with field car_brand_id. I need this field to create relationship, but i have error in this string. How fix this problem?
string brand_query = "SELECT car_brand_id FROM car_brand WHERE name_brand = @brand ";
cmd = new SqlCommand(brand_query, SqlConn);
cmd.Parameters.AddWithValue("@brand", brand);
int car_brand_id = cmd.ExecuteNonQuery(); // There i have an error
string model_query = "INSERT INTO car_model (name_model, car_brand_id) VALUES (@model, @car_brand_id)";
cmd = new SqlCommand(model_query, SqlConn);
cmd.Parameters.AddWithValue("@model", model);
cmd.Parameters.AddWithValue("@car_brand_id", car_brand_id);
cmd.ExecuteNonQuery();
Error message:
The INSERT statement conflicted with the FOREIGN KEY constrains. "FK_car_model_car_b30F848ED". The conflict occured in db "avtoservice:, table "dbo.car_brand", column "car_brand_id". The statument has been terminated