Hi, I'm kinda new to the c# programming and I have an assignment to make a simple app for accounting. So, I made a DB in visual studio, made a new project that is connected to that DB. Table B has a column 'FK_TableA'.
On a form I have put a combobox and through 'use data bound items' connected it like this:
Datasource - TableABindingSource
Displaymember - ID (which is a primary key)
Valuemember - ID
Selectedvalue - TableBBindingSource-FK_TableA

So I should it should take the valuemember from combobox and put it in the column 'FK_TableA' into the table B.

string tekstVeze = "Data Source=.\\SQLEXPRESS; AttachDbFilename = C:\\ProjektBazaNova.mdf; Integrated Security = true; Connect Timeout = 30; User Instance = True";
            SqlConnection veza = new SqlConnection(tekstVeze);
            veza.Open();

       string dat = textBox1.Text;
       string id = comboBox1.ValueMember;
       
       string nar=" INSERT INTO TableB (Dat, FK_TableA) VALUES ('"+dat+"','"+id+"')";
       SqlCommand com = new SqlCommand(nar, veza);
       com.ExecuteNonQuery;

When I start the app and try to insert some data into TableB i get the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TableB_TableA". The conflict occurred in database "C:\PROJEKTBAZANOVA.MDF", table "dbo.TableA", column 'ID'.
The statement has been terminated.

Can anyone please help me, I guess I have an error in the insert statement, but I have no idea how to solve it... :(
anyone?

I guess I have an error in the insert statement

Not necessarily.

A foreign key is a field in a table that is some other table's primary key (field). A foreign key constraint tells that this field has a value which exists as a primary key in the other table.

You get a foreign key (constraint) violation when you insert a value which does not exist in the other table (as a primary key). In your case, check that TableA has a primary key which you are trying to insert as a foreign key to TableB (variable id). Also, you're inserting variable id as a string (..."','"+id+"'...). If the ID field is numeric value, use ..."', "+id+" ... instead.

HTH

Not necessarily.

A foreign key is a field in a table that is some other table's primary key (field). A foreign key constraint tells that this field has a value which exists as a primary key in the other table.

You get a foreign key (constraint) violation when you insert a value which does not exist in the other table (as a primary key). In your case, check that TableA has a primary key which you are trying to insert as a foreign key to TableB (variable id). Also, you're inserting variable id as a string (..."','"+id+"'...). If the ID field is numeric value, use ..."', "+id+" ... instead.

HTH

I understand that, and the combo box was using the values that are already in table A. Anyway, I solved the problem, but thanks for advice :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.