using BiskLog_Point_Of_Sale; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.Properties; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Point_Of_Sale_Managment { public partial class AddUser : Form { users userMain; SqlConnection cn; SqlCommand cm; SqlTransaction transaction; DatabaseConn conn = new DatabaseConn(); public AddUser(users usr) { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); userMain = usr; } private async void Button1_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(txtlogin.Text) && !String.IsNullOrEmpty(txtFirstname.Text) && !String.IsNullOrEmpty(txtSurname.Text) && !String.IsNullOrEmpty(txtpassword.Text) && !String.IsNullOrEmpty(txtconfirmation.Text) && !String.IsNullOrEmpty(txtTelephone.Text) && !String.IsNullOrEmpty(boxAccess.Text)) { if (txtconfirmation.Text.Equals(txtpassword.Text)) { Task task = new Task(createUser); loading.Visible = true; button1.Enabled = false; button2.Enabled = false; task.Start(); int result = await task; if (result == 1) { string title = "Success"; string message = "User account has been created successfully, please advice users to change their passwords after first logins"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); userMain.LoadUsers(); this.Close(); } else if (result == 2) { string title = "Username not available"; string message = "This username has already been taken by another user in this branch or a different branch, change username to create a new user."; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { string title = "Account not created"; string message = "Unable to create user account, please try again later"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } loading.Visible = false; button1.Enabled = true; button2.Enabled = true; } else { string title = "Passwords do not match"; string message = "Passwords do not match, make sure they match to be able to continue"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } else { a1.Visible = true; a2.Visible = true; a3.Visible = true; a4.Visible = true; a5.Visible = true; a6.Visible = true; a7.Visible = true; string title = "Essential fields empty"; string message = "Fields with asterisk have to be filled to continue"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } private void Button2_Click(object sender, EventArgs e) { this.Close(); } public int createUser() { try { cn.Open(); transaction = cn.BeginTransaction(); cm = new SqlCommand("Insert into tblUsers (username,password,firstname,surname,street_address1,street_address2,city," + "state_or_province,telephone,email,access_level,branchID) values (@username,@password,@firstname,@surname,@street_address1,@street_address2,@city," + "@state_or_province,@telephone,@email,@access_level,@branchID)", cn); txtlogin.Invoke(new Action(() => { cm.Parameters.AddWithValue("@username", txtlogin.Text); })); txtpassword.Invoke(new Action(() => { cm.Parameters.AddWithValue("@password", passwordEncryption.Encrypt(txtpassword.Text)); })); txtFirstname.Invoke(new Action(() => { cm.Parameters.AddWithValue("@firstname", txtFirstname.Text); })); txtSurname.Invoke(new Action(() => { cm.Parameters.AddWithValue("@surname", txtSurname.Text); })); txtStreet.Invoke(new Action(() => { cm.Parameters.AddWithValue("@street_address1", txtStreet.Text); })); txtStreet2.Invoke(new Action(() => { cm.Parameters.AddWithValue("@street_address2", txtStreet2.Text); })); txtCityLocale.Invoke(new Action(() => { cm.Parameters.AddWithValue("@city", txtCityLocale.Text); })); txtState.Invoke(new Action(() => { cm.Parameters.AddWithValue("@state_or_province", txtState.Text); })); txtTelephone.Invoke(new Action(() => { cm.Parameters.AddWithValue("@telephone", txtTelephone.Text); })); txtEmail.Invoke(new Action(() => { cm.Parameters.AddWithValue("@email", txtEmail.Text); })); boxAccess.Invoke(new Action(() => { cm.Parameters.AddWithValue("@access_level", (boxAccess.SelectedItem as ComboboxItem).Value.ToString()); })); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); transaction.Commit(); cn.Close(); return 1; } catch (SqlException ex) { if (ex.Number == 2627) { transaction.Rollback(); cn.Close(); return 2; } else { transaction.Rollback(); cn.Close(); return 3; } } catch { transaction.Rollback(); cn.Close(); return 4; } } private void AddUser_Load(object sender, EventArgs e) { boxAccess.Items.Clear(); ComboboxItem combobox = new ComboboxItem(); combobox.Text = "Owner"; combobox.Value = "owner"; boxAccess.Items.Add(combobox); ComboboxItem combobox1 = new ComboboxItem(); combobox1.Text = "Manager"; combobox1.Value = "manager"; boxAccess.Items.Add(combobox1); ComboboxItem combobox2 = new ComboboxItem(); combobox2.Text = "Assistant Manager"; combobox2.Value = "assist"; boxAccess.Items.Add(combobox2); ComboboxItem combobox3 = new ComboboxItem(); combobox3.Text = "Cashier"; combobox3.Value = "cashier"; boxAccess.Items.Add(combobox3); } } }