public async Task<IActionResult> OnGetAsync() { UserName = HttpContext.Session.GetString(SessionKeyName1); UserEmail = HttpContext.Session.GetString(SessionKeyName2); Console.WriteLine("Current session: " + UserName); if (string.IsNullOrEmpty(UserName)) { Console.WriteLine("Session ended"); return RedirectToPage("/UserLoggedIn/SuccessLogIn"); } else { Console.WriteLine("Retrieving modules"); var connectionStringBuilder = new SqliteConnectionStringBuilder(); DatabaseConnect DBCon = new DatabaseConnect(); // your own class and method in DatabaseConnection folder string dbStringConnection = DBCon.DBStringConnection(); connectionStringBuilder.DataSource = dbStringConnection; var connection = new SqliteConnection(connectionStringBuilder.ConnectionString); connection.Open(); var selectCmd = connection.CreateCommand(); selectCmd.CommandText = @"SELECT * FROM Modules ORDER BY ModLevel"; var reader = selectCmd.ExecuteReader(); while (reader.Read()) { Modules mod = new Modules(); mod.ModCode = reader.GetString(1); mod.ModName = reader.GetString(2); mod.ModLevel = reader.GetInt32(3); mod.ModSemester = reader.GetInt32(4); ModRecords.Add(mod); IsSelect.Add(false); } connection.Close(); return Page(); } }
public IActionResult OnPost() { Console.WriteLine("De-Registering Module"); for (int i = 0; i < ModRecords.Count(); i++) { if (IsSelect[i] == true) { Console.WriteLine(ModRecords[i].ModCode); DeRegMod.Add(ModRecords[i]); } } var connectionStringBuilder = new SqliteConnectionStringBuilder(); DatabaseConnect DBCon = new DatabaseConnect(); // your own class and method in DatabaseConnection folder string dbStringConnection = DBCon.DBStringConnection(); connectionStringBuilder.DataSource = dbStringConnection; var connection = new SqliteConnection(connectionStringBuilder.ConnectionString); connection.Open(); for (int i = 0; i < DeRegMod.Count; i++) { var selectCmd = connection.CreateCommand(); selectCmd.CommandText = @"DELETE FROM RegisteredModule WHERE StudenEmail=$email AND ModCode=$modCode"; selectCmd.Parameters.AddWithValue("$email", UserEmail); selectCmd.Parameters.AddWithValue("$modCode", DeRegMod[i].ModCode); selectCmd.Prepare(); selectCmd.ExecuteNonQuery(); } return(RedirectToPage("/UserModules/ViewRegisteredModule")); }
public IActionResult OnGet() { UserName = HttpContext.Session.GetString(SessionKeyName1); UserEmail = HttpContext.Session.GetString(SessionKeyName2); Console.WriteLine("Current session: " + UserName); if (string.IsNullOrEmpty(UserName)) { Console.WriteLine("Session ended"); return(RedirectToPage("/UserLoggedIn/SuccessLogIn")); } else { Console.WriteLine("Retrieving modules"); var connectionStringBuilder = new SqliteConnectionStringBuilder(); DatabaseConnect DBCon = new DatabaseConnect(); // your own class and method in DatabaseConnection folder string dbStringConnection = DBCon.DBStringConnection(); connectionStringBuilder.DataSource = dbStringConnection; var connection = new SqliteConnection(connectionStringBuilder.ConnectionString); connection.Open(); var selectCmd = connection.CreateCommand(); selectCmd.CommandText = @"SELECT ModCode FROM RegisteredModule WHERE StudenEmail=$email ORDER BY ModCode"; selectCmd.Parameters.AddWithValue("$email", UserEmail); var reader = selectCmd.ExecuteReader(); List <String> GetRegMod = new List <string>(); //to get module that registered by the student while (reader.Read()) { GetRegMod.Add(reader.GetString(0)); } for (int i = 0; i < GetRegMod.Count; i++) { var ModCode = GetRegMod[i]; var selectCmd2 = connection.CreateCommand(); selectCmd2.CommandText = @"SELECT ModName FROM Modules WHERE ModCode=$modCode ORDER BY ModCode"; selectCmd2.Parameters.AddWithValue("$modCode", ModCode); var reader2 = selectCmd2.ExecuteReader(); while (reader2.Read()) { Modules rec = new Modules(); rec.ModCode = GetRegMod[i]; rec.ModName = reader2.GetString(0); ModRecords.Add(rec); } IsSelect.Add(false); } return(Page()); } }
public async Task <IActionResult> OnPostAsync() { Console.WriteLine("Registering Module"); for (int i = 0; i < ModRecords.Count(); i++) { if (IsSelect[i] == true) { Console.WriteLine(ModRecords[i].ModName); GetRegMod.Add(ModRecords[i]); } } Console.WriteLine("Registered Module/s : " + GetRegMod.Count()); var connectionStringBuilder = new SqliteConnectionStringBuilder(); DatabaseConnect DBCon = new DatabaseConnect(); // your own class and method in DatabaseConnection folder string dbStringConnection = DBCon.DBStringConnection(); connectionStringBuilder.DataSource = dbStringConnection; var connection = new SqliteConnection(connectionStringBuilder.ConnectionString); connection.Open(); var selectCmd = connection.CreateCommand(); selectCmd.CommandText = @"SELECT ModCode FROM RegisteredModule WHERE StudenEmail=$email"; Console.WriteLine("Email : " + UserEmail); selectCmd.Parameters.AddWithValue("$email", UserEmail); var reader = selectCmd.ExecuteReader(); List <string> CheckModuleCode = new List <string>(); //a variable use be assigned for the CodeModule registered found while (reader.Read()) { CheckModuleCode.Add(reader.GetString(0)); } Console.WriteLine("No of module found : " + CheckModuleCode.Count); connection.Open(); DateTime dd = DateTime.Now; string date = dd.ToString("dd/MM/yyyy"); if (CheckModuleCode.Count == 0) { for (int i = 0; i < GetRegMod.Count; i++) { var selectCmd2 = connection.CreateCommand(); selectCmd2.CommandText = @"INSERT INTO RegisteredModule (StudenEmail, ModCode, Date) VALUES ($email, $MCode, $Date)"; Console.WriteLine("Email : " + UserEmail); Console.WriteLine("Mod Code : " + GetRegMod[i].ModCode); Console.WriteLine("Date : " + date); selectCmd2.Parameters.AddWithValue("$email", UserEmail); selectCmd2.Parameters.AddWithValue("$MCode", GetRegMod[i].ModCode); selectCmd2.Parameters.AddWithValue("$Date", date); selectCmd2.Prepare(); selectCmd2.ExecuteNonQuery(); Console.WriteLine("A record saved"); } } else //some modules already registered. Only new modules will be registered { for (int i = 0; i < GetRegMod.Count; i++) { bool valid = true; for (int j = 0; j < CheckModuleCode.Count; j++) { if (GetRegMod[i].ModCode == CheckModuleCode[j]) { valid = false; Console.WriteLine("Registered module found!" + CheckModuleCode[j]); } } if (valid == true) { var selectCmd2 = connection.CreateCommand(); selectCmd2.CommandText = @"INSERT INTO RegisteredModule (StudenEmail, ModCode, Date) VALUES ($email, $MCode, $Date)"; Console.WriteLine("Email : " + UserEmail); Console.WriteLine("Mod Code : " + GetRegMod[i].ModCode); Console.WriteLine("Date : " + date); selectCmd2.Parameters.AddWithValue("$email", UserEmail); selectCmd2.Parameters.AddWithValue("$MCode", GetRegMod[i].ModCode); selectCmd2.Parameters.AddWithValue("$Date", date); selectCmd2.Prepare(); selectCmd2.ExecuteNonQuery(); Console.WriteLine("A record saved"); } } } connection.Close(); return(RedirectToPage("/UserModules/ViewRegisteredModule", GetRegMod)); }