دوستان علت ارور System.Data.Linq.ForeignKeyReferenceAlreadyHasValu eException: 'Operation is not valid due to the current state of the object.' چیست ؟
دوستان علت ارور System.Data.Linq.ForeignKeyReferenceAlreadyHasValu eException: 'Operation is not valid due to the current state of the object.' چیست ؟
دوست عزیز لطفا کدنویسیت رو قرار بده.
var query2 = (from u in connect.tblClasses where u.ClassID == Convert.ToInt32(radMultiColumnComboBox1.SelectedVa lue) select u).Single();
var query = (from u in connect.tblStudentBasics where u.StudentID == Convert.ToInt32(dataGridView1[0, dataGridView1.CurrentRow.Index].Value) select u).Single();
query.StudentName = txtName.Text;
query.StudentFatherName = txtFather.Text;
query.StudentFamilyName = txtFamilyName.Text;
query.StudentFullName = lblFullName.Text;
query.StudentPhoneNumber = txtPhone.Text;
query.ClassID = Convert.ToInt32(query2.ClassID);
مشکل در مقدار دهی مستقیم کلید خارجی ClassID هست.
var query2= connect.tblClasses.Find( Convert.ToInt32(radMultiColumnComboBox1.SelectedVa lue));
var query= connect.tblStudentBasics.Find(Convert.ToInt32(data GridView1[0, dataGridView1.CurrentRow.Index].Value));
query.StudentName = txtName.Text;
query.StudentFatherName = txtFather.Text;
query.StudentFamilyName = txtFamilyName.Text;
query.StudentFullName = lblFullName.Text;
query.StudentPhoneNumber = txtPhone.Text;
query.tblClass = query2;
First,solve the problem then write the code
Untitled.png
این مشکل رو با متد Find داره
using System.Linq;
اگر باز خطا داد بجای متد Find از متد FirstOrDefault بزار
First,solve the problem then write the code
از
usingSystem.Linq
استفاده میکنم ولی نه متد Find جواب میده و نه متد FirstOrDefault
ClassId پرایمری کی جدول tblClass و StudentId پرایمری کی جدول StudentBasic هست .
var id=Convert.ToInt32(radMultiColumnComboBox1.Selecte dValue);
var query2= connect.tblClasses.FirstOrDefault(c=>c.ClassId==id );
id=Convert.ToInt32(data GridView1[0, dataGridView1.CurrentRow.Index].Value);
var query= connect.tblStudentBasics.FirstOrDefault(c=>c.Stude ntId==id);
First,solve the problem then write the code