0

I am trying to insert data into the table 'table' , but I am stocked with the error of foreign key , 'building_id' from the building table , how do I get through it?

Error prompt:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_user_user".
The conflict occurred in database "C:\USERS\USER\DOWNLOADS\VIUSAL\VIUSAL\ENERGYVIUSAL\ENERGYVIUSAL\DATABASE1.MDF", table "dbo.building", column 'building_id'. The statement has been terminated.

This is the code:

DataRow drowEmployee = dsetEnergyViusal.Tables["Table"].NewRow();
drowEmployee["Name"] = txtName.Text;
drowEmployee["ContactID"] = Convert.ToInt32(txtContactId.Text);
drowEmployee["Company"] = txtCompanyId.Text;
drowEmployee["Building_id"] = txtBuildingID.Text;
drowEmployee["Telephone"] = txtContactNumber.Text;
drowEmployee["Email"] = txtEmail.Text;
drowEmployee["Password"] = txtPassword.Text;

if (radClient.Checked)
   drowEmployee["Client"] = radClient.Checked;
else if (radNews.Checked)
   drowEmployee["News"] = radNews.Checked;

drowEmployee["Telephone"] = txtContactNumber.Text;

dsetEnergyViusal.Tables["Table"].Rows.Add(drowEmployee);

sqldaEnergyViusal.Update(dsetEnergyViusal, "Table");

MessageBox.Show("Record has been successfully added.", "EnergyViusal", MessageBoxButtons.OK, MessageBoxIcon.Information);
User_register_Load(null, null);

2 Answers 2

1

Probably the building ID you are entering in the textbox and being sent in Insert query doesn't exist in the Building table.

Sign up to request clarification or add additional context in comments.

2 Comments

should i create a new insert query for building_id to insert it into the buiding table directly ?
No, you should first check if it exist in building table and then insert it. by the way they best possible way is to update your building table with all the possible records and then just let the user select from a drop down
0

May be you will be added Building_Id as a foreign key in the currently inserting table. to avoid that you have to first insert into Building table and that inserted id has to be used in this insertion ie you have to use a valid building_id in the insertion.

2 Comments

yes i did, add building_id as a foreign key in the table'table', but guessing it wants me to insert into building table first then reference it to the table'table'. But how can i do that ?
@user1114029: the purpose of foreign key is like that, it must be a primary key in some other table. So you can make the list of buildings as a dropdown in the insertion form and insert the selected building id into the table. Otherwise you can do like insert into the building table first. Take the corresponding building id and insert this id as building_id in the second query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.