private void BtnReg_Click(object sender, RoutedEventArgs e) { try { string name = this.txtName.Text; string email = this.txtEmail.Text; string phone = this.txtPhone.Text; string password = this.txtPassword.Text; string roleindex = this.role.SelectedIndex.ToString(); string role = "admin"; if (roleindex == "0") { role = "user"; } // Verification. if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(password)) { MessageBox.Show( "This field can not be empty. Please fill all fields" , "Fail", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (formvalid == false) { MessageBox.Show("Please use valid email format", "Fail", MessageBoxButton.OK, MessageBoxImage.Error); return; } // Save Info. HomeBusinessLogic.SaveInfo(name, email, phone, password, role); // Display Message MessageBox.Show("You are Successfully Registered", "Success", MessageBoxButton.OK, MessageBoxImage.Information); listuser(); } catch (Exception ex) { Console.Write(ex); // Assuming data insert error is only due to duplicate email MessageBox.Show("The email has already been registered. Please use a different email", "Fail", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void btnImport_Click(object sender, RoutedEventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.DefaultExt = ".xlsx"; openfile.Filter = "(.xlsx)|*.xlsx"; //openfile.ShowDialog(); var browsefile = openfile.ShowDialog(); if (browsefile == true) { string txtFilePath = openfile.FileName; Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1);; Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange; int rowCnt = 0; int colCnt = 0; string th = ""; //table header int thname = 0; int themail = 0; int thphone = 0; int thpassword = 99; int throle = 0; for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++) { th = (string)(excelRange.Cells[1, colCnt] as Range).Value2; if (th.Equals("name", StringComparison.InvariantCultureIgnoreCase)) { thname = colCnt; } else if (th.Equals("email", StringComparison.InvariantCultureIgnoreCase)) { themail = colCnt; } else if (th.Equals("phoneNumber", StringComparison.InvariantCultureIgnoreCase)) { thphone = colCnt; } else if (th.Equals("password", StringComparison.InvariantCultureIgnoreCase)) { thpassword = colCnt; } else if (th.Equals("role", StringComparison.InvariantCultureIgnoreCase)) { throle = colCnt; } } for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++) { string cellname = Convert.ToString((excelRange.Cells[rowCnt, thname] as Range).Value2); string cellemail = Convert.ToString((excelRange.Cells[rowCnt, themail] as Range).Value2); string cellphone = Convert.ToString((excelRange.Cells[rowCnt, thphone] as Range).Value2); if (cellphone[0] != '0') { cellphone = "0" + cellphone; } string cellpassword = "******"; if (thpassword != 99) { cellpassword = Convert.ToString((excelRange.Cells[rowCnt, thpassword] as Range).Value2); } string cellrole = Convert.ToString((excelRange.Cells[rowCnt, throle] as Range).Value2); try { HomeBusinessLogic.SaveInfo(cellname, cellemail, cellphone, cellpassword, cellrole); } catch (Exception ex) { Console.Write(ex); // Assuming data insert error is only due to duplicate email MessageBox.Show("Data row:" + rowCnt + ", email: " + cellemail + " is skipped. Email already exists", "Fail", MessageBoxButton.OK, MessageBoxImage.Information); } } MessageBox.Show("Data import successful", "Succesful", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Import excel: Browsefile cancelled", "Fail", MessageBoxButton.OK, MessageBoxImage.Information); } listuser(); }