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 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)); }