{"The INSERT statement conflicted with the FOREIGN KEY constraint
سلام
من در سی شارپ کد زیر رو نوشتم
privatevoid button1_Click(object sender, EventArgs e)
{
SqlConnection con = newSqlConnection("Data Source=.;Initial Catalog=pjt;Integrated Security=True");
con.Open();
SqlCommand com = newSqlCommand();
com.Connection = con;
com.CommandText="SELECT owner.idowner,owner.name,owner.family,automobile.i dam,automobile.plaque ";
com.CommandText+="FROM automobile INNER JOIN owner ON automobile.idowner=owner.idowner ";
com.CommandText+="WHERE name=N'" + txtname.Text + "AND family=N" + txtfamily.Text + "AND plaque=" +txtplaque +"'";
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
idam = dr.GetInt32(1);
con.Close();
}
SqlConnection con1 = newSqlConnection("Data Source=.;Initial Catalog=pjt;Integrated Security=True");
con1.Open();
SqlCommand com1 = newSqlCommand();
com1.Connection = con1;
com1.CommandText = "INSERT INTO functions(idam,rejectable,accepting,credittime,dat e)values('" + idam + "','" + rbaccept.Checked + "','" + rbreject.Checked + "',N'" + credittime.Text + "','" + lbldate.Text + "')";
com1.ExecuteNonQuery();
con1.Close();
}
و با این پیغام خطا مواجه میشم
نقل قول:
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_function_automobile\". The conflict occurred in database \"pjt\", table \"dbo.automobile\", column 'idam'.\r\nThe statement has been terminated."}
از همه بدتر این که در Diagram_pjt اکثر ارتباط هایی که بین جدوالم درست کرده بودم از بین رفت...
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint
معنی این خطا اینه که شما یک کلید خارجی دارید بین دو جدول Functions و automobile روی فیلد های idam. اما این محدودیت اعمال نشده. دلیلشم اینه که احتمالا فیلد idam که دارید insert میکنید داخل جدول Functions توی جدول automobile وجود نداره.
یک سوال این خط کد قراره چی به شما برگردونه؟
idam = dr.GetInt32(1);
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint
نقل قول:
یک سوال این خط کد قراره چی به شما برگردونه؟
کد:
idam = dr.GetInt32(1);
می خوام فیلد idam رو از جدول automobile برام برگردونه...
در واقع من دستور SELECT رو نوشتم برای اینکه مقدار idam رو به دست بیارم
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint
نقل قول:
نوشته شده توسط
shima2006
می خوام فیلد idam رو از جدول automobile برام برگردونه...
در واقع من دستور SELECT رو نوشتم برای اینکه مقدار idam رو به دست بیارم
خب منم به همین خاطر سوال کردم٬ شما نوشتید GetInt32(1)
در حالی که idam در دستور select شما ستون چهارمه.
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint
mn_zandy63 عزیز من کد رو به این شکل تغییر دادم
SqlConnection con = newSqlConnection("Data Source=.;Initial Catalog=pjt;Integrated Security=True");
con.Open();
SqlCommand com = newSqlCommand();
com.Connection = con;
com.CommandText = "SELECT automobile.idam,owner.name,owner.family,automobile .plaque FROM automobile INNER JOIN owner ON automobile.idowner=owner.idowner WHERE name=N'" + txtname.Text + "'AND family=N'" + txtfamily.Text + "'AND plaque='" + txtplaque + "' ";
SqlDataReader dr = com.ExecuteReader();
bool hasrows = dr.HasRows;
if (!hasrows)
{
MessageBox.Show("not found");
con.Close();
}
اون خطا همچنان پابرجاست ولی من اینطوری جلویه اون خطا رو گرفتم و حالا موقع اجرا پیغام not found رو به من نمایش میده و این در حالیست که من این کوئری رو درSQL SERVER Query به درستی اجرا می گیرم و idam رو هم اونجا بهم برمی گردونه ولی وقتی در سی شارپ می خوام اجرا بگیرم اینطوری میشه:متفکر:
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint
يه کار کن
روی خط SqlDataReader dr = com.ExecuteReader();
یک Breakpoint بذار با F9 بعد برنامه رو اجرا کن٬ به این خط که رسید٬ ماوس ت رو ببر روی com.CommandText و ببین مقدارش چیه٬ اینجا بذارش٬ و همون رو توی خود SQL چک کن٬ ببین درست بهت جواب میده؟
البته اگه قبلا این کار رو انجام ندادی.
Happy Coding...
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint
متشکرم ، مشکل حل شد
دلیلش هم در کد بالا مشخصه به جایه txtplaue.text نوشته بودم txtplapue:اشتباه:
ولی به جاش استفاده کردن از F9 رو یاد گرفتم، خیلی خوب بود:چشمک:
نقل قول: {"The INSERT statement conflicted with the FOREIGN KEY constraint