Hi
I am very new to C# and learning as I go along
I am writing a student app
So what I am trying to accomplish is the following:
I have a table with modules linked to a subject. So when a student registers a subject I want the modules to automatically be written to the StudyHistory table.
I have tried the following script, it is not the best I am sure but I learn more and more each day
Any history will be appreciated
I am very new to C# and learning as I go along
I am writing a student app
So what I am trying to accomplish is the following:
I have a table with modules linked to a subject. So when a student registers a subject I want the modules to automatically be written to the StudyHistory table.
I have tried the following script, it is not the best I am sure but I learn more and more each day
Any history will be appreciated
C#:
conn = new SqlConnection(cs.DBConn);
conn.Open();
string querySelectedStudentModule = "select ModuleNumber,ModuleName from Modules where fkSubjectNumber='" + studentCourseSubject + "' and Archived = '0'";
SqlCommand cmdSelectedStudentModule = new SqlCommand(querySelectedStudentModule);
cmdSelectedStudentModule.Connection = conn;
SqlDataReader rdrSelectedStudentModule = cmdSelectedStudentModule.ExecuteReader();
////// Loop through the result set
while (rdrSelectedStudentModule.Read())
{
studentModule = rdrSelectedStudentModule["ModuleNumber"].ToString();
studentModuleName = rdrSelectedStudentModule["ModuleName"].ToString();
int incrementStudentNumber = subjectPaymentNumber + 1;
string convertedNumber = incrementStudentNumber.ToString().PadLeft(6, '0');
string completeHistoryNumber = "HIS" + convertedNumber;
string queryInsertStudyHistory = "insert into StudyHistory (StudyHistoryNumber,StudentNumber,CourseNumber,CourseName,SubjectNumber,SubjectName,ModuleNumber,ModuleName) " +
"VALUES (@StudyHistoryNumber,@StudentNumber,@CourseNumber,@CourseName,@SubjectNumber,@SubjectName,@ModuleNumber,@ModuleName)";
SqlCommand cmdInsertStudyHistory = new SqlCommand(queryInsertStudyHistory);
cmdInsertStudyHistory.Connection = conn;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@StudyHistoryNumber", System.Data.SqlDbType.NVarChar, 50, "StudyHistoryNumber"));
cmdInsertStudyHistory.Parameters["@StudyHistoryNumber"].Value = completeHistoryNumber;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@StudentNumber", System.Data.SqlDbType.NVarChar, 50, "StudentNumber"));
cmdInsertStudyHistory.Parameters["@StudentNumber"].Value = studentNumberUpdater;
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 = studentSelectedCourseName;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@SubjectNumber", System.Data.SqlDbType.NVarChar, 50, "SubjectNumber"));
cmdInsertStudyHistory.Parameters["@SubjectNumber"].Value = studentCourseSubject;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@SubjectName", System.Data.SqlDbType.NVarChar, 250, "SubjectName"));
cmdInsertStudyHistory.Parameters["@SubjectName"].Value = studentSelectedSubjectName;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleNumber", System.Data.SqlDbType.NVarChar, 50, "ModuleNumber"));
cmdInsertStudyHistory.Parameters["@ModuleNumber"].Value = studentModule;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleName", System.Data.SqlDbType.NVarChar, 250, "ModuleName"));
cmdInsertStudyHistory.Parameters["@ModuleName"].Value = studentModuleName;
cmdInsertStudyHistory.ExecuteReader();
// close reader
}
rdrSelectedStudentModule.Close();
conn.Close();
Last edited by a moderator: