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... :(