分数管理界面不能写入课程编号,其余均没问题,怎么回事?求大婶解救
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 学生成绩管理系统;
//看一眼你代码的对象画红线的,右键 解析 引入命名空间
namespace 学生成绩管理系统
{
public partial class ScoreMsgFrm : Form
{
private List<MySchool.StudentMsg> StudentList;
private List<MySchool.CourseMsg> CourseList;
private List<MySchool.ScoreMsg> ScoreList;
private int current;
private MySchool.MySchoolDBContext db;
public ScoreMsgFrm()
{
InitializeComponent();
StudentList = new List<MySchool.StudentMsg>();//初始化当前实体对象集合
CourseList = new List<MySchool.CourseMsg>();
ScoreList = new List<MySchool.ScoreMsg>();
current = 1; //默认的当前图书编号
string sqlConn = @"Data Source=Win-01412261307;Initial Catalog=MySchool;Integrated Security=true";
db = new MySchool.MySchoolDBContext(sqlConn);
}
private void showScore()//显示当前学生信息
{
if (current >= 1 && current <= ScoreList.Count)
{
txtCourseId.Text = ScoreList[current - 1].CourseId.ToString();
txtStudentId.Text = ScoreList[current - 1].StudentNo.ToString();
txtScore.Text = ScoreList[current - 1].Score.ToString();
}
}
private void ScoreMsgFrm_Load(object sender, EventArgs e)
{
var queryScore = from score in db.scores select score; //定义查询语句
foreach (var score in queryScore)//执行LINQ查询
{
ScoreList.Add(score);
}
showScore();//显示当前成绩
}
private void btnPreviouss_Click(object sender, EventArgs e)
{
if (current == 1)
{
MessageBox.Show("已经到第一条了", "注意", MessageBoxButtons.OK);
}
else
{
current--;
showScore();
};
}
private void btnNext_Click(object sender, EventArgs e)
{
if (current == ScoreList.Count)
{
MessageBox.Show("已经到最后一条了", "注意", MessageBoxButtons.OK);
}
else
{
current++;
showScore();
};
}
private void txtCourseId_TextChanged(object sender, EventArgs e)
{
var queryCourse = //定义LINQ查询,获得指定的课程名称
from course in db.courses
where course.CourseId == int.Parse(txtCourseId.Text)
select course;
foreach (var course in queryCourse)//执行LINQ查询
{
txtCourseName.Text = course.CourseName;//显示课程名称
}
}
private void txtStudentId_TextChanged(object sender, EventArgs e)
{
var queryStudent = //定义LINQ查询,获得指定的姓名
from student in db.students
where student.StudentNo == int.Parse(txtStudentId.Text)
select student;
foreach (var course in queryStudent)//执行LINQ查询
{
学生成绩管理系统.MySchool.StudentMsg student = new 学生成绩管理系统.MySchool.StudentMsg();
txtStudentName.Text =student.StudentName;//显示学生姓名
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
MySchool.ScoreMsg score = new MySchool.ScoreMsg(int.Parse(txtCourseId.Text),
int.Parse(txtStudentId.Text), int.Parse(txtScore.Text));//创建ScoreMsg对象,封装用户输入
db.scores.InsertOnSubmit(score);//将ScoreMsg对象添加到table对象中
try
{
db.SubmitChanges();//将更新结果提交给DMS
ScoreList.Add(score);//在实体集合中添加新ScoreMsg对象
current = ScoreList.Count;
showScore(); //显示新添加的图书
MessageBox.Show("已经到成功添加目录", "注意", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "注意", MessageBoxButtons.OK);
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (current >= 1 && current <= ScoreList.Count)
{
var updateScores = //定义LINQ查询
from score in db.scores
where (score.CourseId == ScoreList[current - 1].CourseId && score.StudentNo == ScoreList[current - 1].StudentNo)
select score;
foreach (MySchool.ScoreMsg score in updateScores)//执行LINQ查询
{//把用户输入的新成绩赋值给实体对象的对应属性
score.Score = int.Parse(txtScore.Text);
//注意,由于课程号和学号是成绩表的主键,因此原则上不能修改
}
try
{
db.SubmitChanges();//将更新结果提交给DBMS
MessageBox.Show("已经到成功更新记录", "注意", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "注意", MessageBoxButtons.OK);
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (current >= 1 && current <= ScoreList.Count)
{
var delScores = //定义LINQ查询
from score in db.scores
where (score.CourseId == ScoreList[current - 1].CourseId && score.StudentNo == ScoreList[current - 1].StudentNo)
select score;
foreach (MySchool.ScoreMsg score in delScores)//执行LINQ查询
{
db.scores.DeleteOnSubmit(score);//删除Table对象中的指定对象
}
try
{
db.SubmitChanges();//将更新结果提交给DBMS
ScoreList.RemoveAt(current - 1);
if (current > 0) current--;
showScore();
MessageBox.Show("已经到成功删除记录", "注意", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "注意", MessageBoxButtons.OK);
}
}
}
}
}
------解决思路----------------------
哪行代码报错?
------解决思路----------------------
调试一下,看queryCourse是否为null?