Hi
I am busy with my student app where one subject consists of one or more modules.
I have a database table that groups modules by the subjects that they belong to so when a student is added and they are registered for a subject it should add all the modules for that subject to their profile.
I start by querying the database to get a list of all the modules for a particular subject
I am getting an error that says there is already an open data reader. I think it is because when I am calling this line " InsertStudyHistory(comboBoxDatabaseConnection, studentModule);" , the data reader rdrSelectedStudentModule is still open. I am not sure how to solve this
I have code for the InsertStudyHistory
Any assistance will be appreciated
I am busy with my student app where one subject consists of one or more modules.
I have a database table that groups modules by the subjects that they belong to so when a student is added and they are registered for a subject it should add all the modules for that subject to their profile.
I start by querying the database to get a list of all the modules for a particular subject
C#:
comboBoxDatabaseConnection.Open();
using (SqlCommand cmdInsertStudyHistory = new SqlCommand("select ModuleNumber,ModuleName,ModuleCode from Modules where fkSubjectNumber=@subjectNumber and Archived = '0'", comboBoxDatabaseConnection))
{
cmdInsertStudyHistory.Parameters.AddWithValue("@subjectNumber", studentCourseSubject);
SqlDataReader rdrSelectedStudentModule = cmdInsertStudyHistory.ExecuteReader();
if (rdrSelectedStudentModule.HasRows)
{
while (rdrSelectedStudentModule.Read())
{
studentModule = rdrSelectedStudentModule["ModuleNumber"].ToString();
studentModuleName = rdrSelectedStudentModule["ModuleName"].ToString();
studentModuleCode = rdrSelectedStudentModule["ModuleCode"].ToString();
InsertStudyHistory(comboBoxDatabaseConnection, studentModule);
}
}
rdrSelectedStudentModule.Close();
comboBoxDatabaseConnection.Close();
}
I have code for the InsertStudyHistory
C#:
public void InsertStudyHistory(SqlConnection comboBoxDatabaseConnection, string studentModule)
{
using (SqlCommand cmdInsertStudyHistory = new SqlCommand("insert into StudentModule (StudentNumber,CourseNumber,CourseName,SubjectNumber,SubjectName,ModuleNumber,ModuleCode,ModuleName) " +
"VALUES (@StudentNumber,@CourseNumber,@CourseName,@SubjectNumber,@SubjectName,@ModuleNumber,@ModuleCode,@ModuleName)", comboBoxDatabaseConnection))
{
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@StudentNumber", System.Data.SqlDbType.NVarChar, 50, "StudentNumber"));
cmdInsertStudyHistory.Parameters["@StudentNumber"].Value = selectedStudentNumber;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@CourseNumber", System.Data.SqlDbType.NVarChar, 50, "CourseNumber"));
cmdInsertStudyHistory.Parameters["@CourseNumber"].Value = selectedStudentSubject;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@CourseName", System.Data.SqlDbType.NVarChar, 250, "CourseName"));
cmdInsertStudyHistory.Parameters["@CourseName"].Value = editStudentSubjectProgrammeComboBox.Text.ToString();
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@SubjectNumber", System.Data.SqlDbType.NVarChar, 50, "SubjectNumber"));
cmdInsertStudyHistory.Parameters["@SubjectNumber"].Value = editStudentSubjectComboBox.SelectedValue.ToString(); ;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@SubjectName", System.Data.SqlDbType.NVarChar, 250, "SubjectName"));
cmdInsertStudyHistory.Parameters["@SubjectName"].Value = editStudentSubjectComboBox.Text.ToString();
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleNumber", System.Data.SqlDbType.NVarChar, 50, "ModuleNumber"));
cmdInsertStudyHistory.Parameters["@ModuleNumber"].Value = studentModule;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleCode", System.Data.SqlDbType.NVarChar, 50, "ModuleCode"));
cmdInsertStudyHistory.Parameters["@ModuleCode"].Value = studentModuleCode;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleName", System.Data.SqlDbType.NVarChar, 250, "ModuleName"));
cmdInsertStudyHistory.Parameters["@ModuleName"].Value = studentModuleName;
cmdInsertStudyHistory.ExecuteNonQuery();
}
comboBoxDatabaseConnection.Close();
}
}
Any assistance will be appreciated