我尝试加入表格学生和课程,然后使用SID将标记(按下添加按钮时)添加到表格 Student_Course (学生ID)和CID(课程ID)组合框,绑定到学生中的表SID和课程中的CID。
在代码中我试图加入表格,但它给了我一个
"未处理的类型' System.InvalidOperationException'发生在System.Core.dll"`错误。我加入表格时出错。
我该如何解决这个问题?还是有另一种方法可以做到这一点吗?
TLDR;
所以我有两个名为CID和SID的组合框,我可以选择课程ID和学生ID。然后我有一个Marks的文本框。我希望能够选择课程ID和学生ID,然后将标记添加到其中。
private void btnAdd_Click(object sender, EventArgs e)
{
using (Entities2 db = new Entities2())
{
int SID = Convert.ToInt32(cboSID.Text);
int CID = Convert.ToInt32(cboCID.Text);
Student_Course marks = (from c in db.Student_Course join s in db.Students on c.SID equals s.SID join o in db.Courses on c.CID equals o.CID where c.SID == SID && c.CID == CID select c).First();
marks.Mark = Convert.ToInt32(txtMark.Text);
db.SaveChanges();
MessageBox.Show("Marks Added");
}
using (Entities2 db = new Entities2()) //link combobox and DataGridView to List in Student_Course
{
var studentCourse = from c in db.Student_Course select new { SID = c.SID, Mark = c.Mark };
editDataGridView.DataSource = studentCourse.ToList();
}
using (Entities2 db = new Entities2())
{
var student = (from s in db.Students select s).ToList();
cboSID.DisplayMember = "SID";
cboSID.ValueMember = "SID";
cboSID.DataSource = student;
}
using (Entities2 db = new Entities2())
{
var course = (from c in db.Courses select c).ToList();
cboCID.DisplayMember = "CID";
cboCID.ValueMember = "CID";
cboCID.DataSource = course;
}
}

答案 0 :(得分:2)
private void btnAdd_Click(object sender, EventArgs e)
{
using (Entities2 db = new Entities2())
{
int SID = Convert.ToInt32(cboSID.SelectedItem.ToString());
int CID = Convert.ToInt32(cboCID.SelectedItem.ToString());
int got_marks = Convert.ToInt32(txtMark.Text);
Student_Course marks = db.Student_Course.Where(sc => sc.SID == SID && c.CID == CID ).FirstOrDefault();
if( marks!= null ){
marks.Mark = got_marks;
}else{
db.Student_Course.Add(new Student_Course{
SID = SID,
CID = CID,
Mark = got_marks
});
}
db.SaveChanges();
MessageBox.Show("Marks Added");
}
using (Entities2 db = new Entities2()) //link combobox and DataGridView to List in Student_Course
{
var studentCourse = from c in db.Student_Course select new { SID = c.SID, Mark = c.Mark };
editDataGridView.DataSource = studentCourse.ToList();
}
using (Entities2 db = new Entities2())
{
var student = (from s in db.Students select s).ToList();
cboSID.DisplayMember = "SID";
cboSID.ValueMember = "SID";
cboSID.DataSource = student;
}
using (Entities2 db = new Entities2())
{
var course = (from c in db.Courses select c).ToList();
cboCID.DisplayMember = "CID";
cboCID.ValueMember = "CID";
cboCID.DataSource = course;
}
}
答案 1 :(得分:0)
using (Entities2 db = new Entities2())
{
int SID = Convert.ToInt32(cboSID.SelectedValue);//Change cboSID.Text to cboSID.SelectedValue
int CID = Convert.ToInt32(cboCID.SelectedValue);//Change cboCID.Text to cboCID.SelectedValue
Student_Course marks = (from c in db.Student_Course join s in db.Students on c.SID equals s.SID join o in db.Courses on c.CID equals o.CID where c.SID == SID && c.CID == CID select c).First();
marks.Mark = Convert.ToInt32(txtMark.Text);
//Delete this line db.SaveChanges();
MessageBox.Show("Marks Added");
}