0

データベース層..................

public static void AddEnrollment(StudentScore enrollment)
{
       SqlConnection conn = MyDB.GetConnection();

       string insertStm = "insert into EnrollmentTable (StudentID,CourseID,DateEnrolled,Score) values (@StudentID,@CourseID,@DateEnrolled,@Score)";

       SqlCommand insertComm = new SqlCommand(insertStm, conn);

       insertComm.Parameters.AddWithValue("@StudentID", enrollment.EnrollmentData.StudentData.StudentID);
       insertComm.Parameters.AddWithValue("@CourseID", enrollment.EnrollmentData.CourseData.CourseID);
       insertComm.Parameters.AddWithValue("@DateEnrolled", enrollment.EnrollmentData.DOE);
       insertComm.Parameters.AddWithValue("@Score", enrollment.Score);

       try
       {
           conn.Open();
           insertComm.ExecuteNonQuery();
       }
       catch (SqlException sqlex)
       {
           throw sqlex;
       }
       finally
       {
           conn.Close();
       }

ビジネス層.................

public class StudentScore
{
    public Enrollment EnrollmentData { get; set; }
    public int Score { get; set; }

    public StudentScore()
    {
    }

    public StudentScore(Enrollment aEnrollmentData, int aScore)
    {
        EnrollmentData = aEnrollmentData;
        Score = aScore;
    }
}

プレゼンテーション層.................................

private void button5_Click(object sender, EventArgs e)
{
        StudentScore enrollment = new StudentScore();

        enrollment.EnrollmentData.StudentData.StudentID = comboBox1.SelectedValue.ToString();
        enrollment.EnrollmentData.CourseData.CourseID = comboBox2.SelectedValue.ToString();
        enrollment.EnrollmentData.DOE = DateTime.Now;

        EnrollmentDB.AddEnrollment(enrollment);

        MessageBox.Show("Student is enrolled this course.");
    }   

このエラーが表示されます

例外: 登録.登録データ.学生データ.学生ID = コンボボックス1.選択値.ToString();

どうすれば修正できますか?

4

1 に答える 1

1

enrollment.EnrollmentData初期化されていません。詳細がない場合は変更

StudentScore enrollment = new StudentScore();

StudentScore enrollment = new StudentScore(new Enrollment(), 0);
于 2012-07-27T15:28:15.733 に答える