Hello :) I have a problem for a couple of weeks and I really don't know how to fix it. I have 2 tables
clienti(id_client(pk),nume, prenume, id_utilizator(fk)) and
utilizator(id_utilizator(pk), email, id_client(fk))
When i'm trying to add a new record I keep getting "The UPDATE statement conflicted with the Foreign Key constraint "FK_client_utilizator etc". Does anybody know how to fix it? PLEASE it drives me nuts :(.
I've set for id_utilizator(fk) from clienti a DEFAULT VALUE that is found on the PK domain in utilizator. I can add client information, but of course id_utilizator is the DEFAULT VALUE. When I'm trying to update it, I get that error. Here's my code:
//adding information in client
string tip_client = null;
string tip_persoana = null;
string judet = null;
if (tipClientCb.SelectedItem.ToString() == "Personal")
tip_client = "personal";
else
if (tipClientCb.SelectedItem.ToString() == "Altul")
tip_client = "altul";
if (tipPersCb.SelectedItem.ToString() == "Persoana fizica")
tip_persoana = "persoana fizica";
else
if (tipPersCb.SelectedItem.ToString() == "Persoana juridica")
tip_persoana = "persoana juridica";
judet = judetCb.SelectedItem.ToString();
string insertClient = "INSERT INTO client (tip_client,tip_persoana,cuicnp,denumire,telefon,localitate,judet) VALUES ('"
+ tip_client + "','" + tip_persoana + "','" + codClientTxt.Text + "','" + denumireTxt.Text + "','" + telefonTxt.Text + "','" + localitateTxt.Text+"','" + judet + "')";
System.Data.SqlClient.SqlCommand executaInsertClient = new System.Data.SqlClient.SqlCommand(insertClient, conn);
executaInsertClient.ExecuteNonQuery();
}
//saving id_client
int id_client;
string selectIdClient = "SELECT * FROM client WHERE tip_client='" + tipClientCb.Text +
"' AND tip_persoana='" + tipPersCb.Text +
"' AND cuicnp='" + codClientTxt.Text +
"' AND denumire='" + denumireTxt.Text +
"' AND localitate='" + localitateTxt.Text +
"' AND judet='" + judet + "'";
System.Data.SqlClient.SqlCommand executaSelectIdClient = new System.Data.SqlClient.SqlCommand(selectIdClient, conn);
System.Data.SqlClient.SqlDataReader rdr_id_client = executaSelectIdClient.ExecuteReader();
rdr_id_client.Read();
id_client = Convert.ToInt32(rdr_id_client["id_client"]);
rdr_id_client.Close();
//adding information in utilizator table
string insertUtilizator = "INSERT INTO utilizator (id_utilizator, id_client, parola,[e-mail]) VALUES ('"
+ utilizatorTxt.Text + "','" + id_client + "','" + parolaTxt.Text + "','" + emailTxt.Text + "')";
System.Data.SqlClient.SqlCommand executaInsertUtilizator = new System.Data.SqlClient.SqlCommand(insertUtilizator, conn);
executaInsertUtilizator.ExecuteNonQuery();
//updating id_utilizator
string updateIdUtilizator = "UPDATE client SET id_utilizator='" + utilizatorTxt.Text + " WHERE id_client = " + id_client + "'";
System.Data.SqlClient.SqlCommand executaUpdateIdUtilizator = new System.Data.SqlClient.SqlCommand(updateIdUtilizator, conn);
executaUpdateIdUtilizator.ExecuteNonQuery();
Loading

Violeta PopaPosted Apr 2, 2013, 5:01 AM
Jignesh TrivediPosted Apr 2, 2013, 12:14 AM
I think there is problem with the code in which you get id_client.
also I have some couple of question.
are you use identity column as PK?
what is default value in your case?
Violeta PopaPosted Apr 1, 2013, 10:31 AM
client( id_client(pk), nume, prenume, id_utilizator(fk))
utilizator(id_utilizator(pk), email, id_client(fk))
i don't have null values, i have the default value.
Arul ManivannanPosted Apr 1, 2013, 7:38 AM
This error on foreign key constraints normally occur when the values you are going to update has null values. Please check whether there will be no null values in update statement, Eliminate null values before updating.
- Arul Manivannan
Jignesh TrivediPosted Apr 1, 2013, 5:57 AM
can you please share table structure with FK and PK detail?
this will us to resolve your problem.
Violeta PopaPosted Apr 1, 2013, 3:37 AM
Jignesh TrivediPosted Apr 1, 2013, 12:35 AM
hi,
this type of error due to trying to insert invalid data with in foreign key column.
for example if your foreign key column is nullable and you try to insert zero (0) instead of null
and also i think, here default value is help you.
trying to insert value with in range of utilizator table
hope this will help you.