//To Insert user Information public static int InsertUserInformation(BusinessLayer.UserInformation ui) { int result; int newId = -1; using (SqlConnection cnn = GetSqlConnection()) { String sql = $"Insert into UserInformation(FirstName,LastName,Address,City,Province,PostalCode,Country) values ('{ui.FirstName}','{ui.LastName}','{ui.Address}','{ui.City}','{ui.Province}','{ui.PostalCode}','{ui.Country}')"; using (SqlCommand command = new SqlCommand(sql, cnn)) { //open connection cnn.Open(); command.CommandType = System.Data.CommandType.Text; command.CommandText = sql; //execute query result = command.ExecuteNonQuery(); //select id of newly added record string query2 = "Select @@Identity as newId from UserInformation"; command.CommandText = query2; newId = Convert.ToInt32(command.ExecuteScalar()); } } return(newId); }
public static BusinessLayer.UserInformation SelectUserInformationById(int userId) { BusinessLayer.UserInformation userInformation = new BusinessLayer.UserInformation(); using (SqlConnection cnn = GetSqlConnection()) { String sql = $"Select * From UserInformation Where Id = {userId}"; using (SqlCommand command = new SqlCommand(sql, cnn)) { cnn.Open(); using (SqlDataReader dataReader = command.ExecuteReader()) { while (dataReader.Read()) { userInformation.Id = (int)dataReader.GetValue(0); userInformation.FirstName = (string)dataReader.GetValue(1); userInformation.LastName = (string)dataReader.GetValue(2); userInformation.Address = (string)dataReader.GetValue(3); userInformation.City = (string)dataReader.GetValue(4); userInformation.Province = (string)dataReader.GetValue(5); userInformation.PostalCode = (string)dataReader.GetValue(6); userInformation.Country = (string)dataReader.GetValue(7); } } } } return(userInformation); }
public static int InsertUserInformation(BusinessLayer.UserInformation ui) { int newId = default; using (SqlConnection cnn = GetSqlConnection()) { String sql = $"Insert into UserInformation(FirstName,LastName,Address,City,Province,PostalCode,Country) values ('{ui.FirstName}','{ui.LastName}','{ui.Address}','{ui.City}','{ui.Province}','{ui.PostalCode}','{ui.Country}');SELECT SCOPE_IDENTITY()"; using (SqlCommand command = new SqlCommand(sql, cnn)) { cnn.Open(); command.CommandType = System.Data.CommandType.Text; command.CommandText = sql; //result = command.ExecuteNonQuery(); //Executes the query, and get the id of the new user. Object returnObj = command.ExecuteScalar(); if (returnObj != null) { int.TryParse(returnObj.ToString(), out newId); } cnn.Close(); } } return(newId); }
private void buttonRetireUser_Click(object sender, EventArgs e) { //Check if any User is selected. if (dataGridViewUsers.SelectedCells.Count > 0) { //Save current row index to reselect this row later. int savedRowIndex = (dataGridViewUsers.CurrentRow.Index); //Get selected user's information. BusinessLayer.UserInformation userInfo = new BusinessLayer.UserInformation(); userInfo.UserID = (int)(dataGridViewUsers.Rows[dataGridViewUsers.CurrentCell.RowIndex].Cells[0].Value); userInfo.Username = (string)(dataGridViewUsers.Rows[dataGridViewUsers.CurrentCell.RowIndex].Cells[1].Value); userInfo.Role = (string)(dataGridViewUsers.Rows[dataGridViewUsers.CurrentCell.RowIndex].Cells[3].Value); if (dataGridViewUsers.Rows[dataGridViewUsers.CurrentCell.RowIndex].Cells[4].Value != null) userInfo.DateRetired = (DateTime)(dataGridViewUsers.Rows[dataGridViewUsers.CurrentCell.RowIndex].Cells[4].Value); //Create and display the form for managing retirement. FormAdminRetireUser formAdminRetireUser = new FormAdminRetireUser(userInfo); DialogResult res = formAdminRetireUser.ShowDialog(this); formAdminRetireUser.Dispose(); //Refresh displayed Users. searchForUsers(); //Select previously selected user. dataGridViewUsers.CurrentCell = dataGridViewUsers[0, savedRowIndex]; } else { MessageBox.Show("Please select a User to manage retirement.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public static int InsertUserInformation(BusinessLayer.UserInformation ui) { int result = 0; using (SqlConnection cnn = GetSqlConnection()) { String sql = $"Insert into UserInformation(FirstName,LastName," + $"Address,City,Province,PostalCode,Country)" + $"values ('{ui.FirstName}','{ui.LastName}','{ui.Address}'," + $"'{ui.City}','{ui.Province}','{ui.PostalCode}','{ui.Country}') select SCOPE_IDENTITY() as 'myNewId'"; using (SqlCommand command = new SqlCommand(sql, cnn)) { using (SqlDataAdapter adapter = new SqlDataAdapter()) { cnn.Open(); adapter.InsertCommand = new SqlCommand(sql, cnn); SqlDataReader idReader; //result = adapter.InsertCommand.ExecuteNonQuery(); idReader = adapter.InsertCommand.ExecuteReader(); while (idReader.Read()) { insertedUserId = int.Parse(idReader.GetValue(0).ToString()); } if (idReader.HasRows == true) { result = 1; } ; } } } return(result); }
private void buttonAddUser_Click(object sender, EventArgs e) { //Check if information is correct. if (!correctInformation()) { return; } //Check if user already exists in database. if (BusinessLayer.AdminFacade.ExistsUser(textBoxUsername.Text.Trim())) { MessageBox.Show("User with Username '" + textBoxUsername.Text + "' already exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //Utworzenie Usera oraz danej Roli. int PWZNumber = (comboBoxRole.Text == "DOC") ? int.Parse(textBoxPWZNumber.Text) : 0; BusinessLayer.AdminFacade.AddUser(textBoxUsername.Text.Trim(), textBoxPassword.Text, comboBoxRole.Text, textBoxFirstName.Text.Trim(), textBoxLastName.Text.Trim(), PWZNumber); newUserInformation = new BusinessLayer.UserInformation() { Username = textBoxUsername.Text }; DialogResult = DialogResult.OK; }
public FormAdminChangePassword(BusinessLayer.UserInformation userInformation) { InitializeComponent(); //Set window title. this.Text = "Change Password"; this.userInformation = userInformation; textBoxUserID.Text = userInformation.UserID.ToString(); textBoxUsername.Text = userInformation.Username; textBoxRole.Text = userInformation.Role; }
public static int InsertUserInformation(BusinessLayer.UserInformation userInfo) { int result; using (SqlConnection cnn = GetSqlConnection()) { String sql = $"INSERT INTO UserInformation(FirstName, LastName, Address, City, Province, PostalCode, Country) VALUES ('{userInfo.FirstName}', '{userInfo.LastName}', '{userInfo.Address}', '{userInfo.City}', '{userInfo.Province}', '{userInfo.PostalCode}', '{userInfo.Country}')"; using (SqlCommand command = new SqlCommand(sql, cnn)) { using (SqlDataAdapter adapter = new SqlDataAdapter()) { cnn.Open(); adapter.InsertCommand = new SqlCommand(sql, cnn); result = adapter.InsertCommand.ExecuteNonQuery(); } } } return(result); }
public static int UpdateUserInformationById(int userId, BusinessLayer.UserInformation ui) { int result; using (SqlConnection cnn = GetSqlConnection()) { String sql = $"Update UserInformation Set FirstName='{ui.FirstName}',LastName='{ui.LastName}',Address='{ui.Address}',City='{ui.City}',Province='{ui.Province}',PostalCode='{ui.PostalCode}',Country='{ui.Country}' Where Id = {userId}"; using (SqlCommand command = new SqlCommand(sql, cnn)) { cnn.Open(); command.CommandType = System.Data.CommandType.Text; command.CommandText = sql; result = command.ExecuteNonQuery(); } } return(result); }
public static int InsertUserInformation(BusinessLayer.UserInformation ui) { int result; using (SqlConnection cnn = GetSqlConnection()) { String sql = $"Insert into UserInformation(FirstName,LastName,Address,City,Province,PostalCode,Country) values ('{ui.FirstName}','{ui.LastName}','{ui.Address}','{ui.City}','{ui.Province}','{ui.PostalCode}','{ui.Country}')"; using (SqlCommand command = new SqlCommand(sql, cnn)) { cnn.Open(); command.CommandType = System.Data.CommandType.Text; command.CommandText = sql; result = command.ExecuteNonQuery(); } } return(result); }
private void searchForUsers() { BusinessLayer.UserInformation userSearchCriteria = new BusinessLayer.UserInformation(); userSearchCriteria.UserID = (textBoxUserID.Text=="" ? 0 : int.Parse(textBoxUserID.Text)); userSearchCriteria.Username = textBoxUsername.Text; if (!string.IsNullOrWhiteSpace(comboBoxRole.Text)) userSearchCriteria.Role = comboBoxRole.Text; if (dateTimePickerDateRetired.Checked) userSearchCriteria.DateRetired = dateTimePickerDateRetired.Value.Date; dataGridViewUsers.Columns.Clear(); dataGridViewUsers.DataSource = BusinessLayer.AdminFacade.GetUsers(userSearchCriteria, checkBoxOnlyActive.Checked); dataGridViewUsers.Columns[0].Width = 60; dataGridViewUsers.Columns[1].Width = 130; dataGridViewUsers.Columns[2].Width = 143; dataGridViewUsers.Columns[3].Width = 60; dataGridViewUsers.Columns[4].Width = 110; //Select first user if not empty. if (dataGridViewUsers.Rows.Count > 0) dataGridViewUsers.CurrentCell = dataGridViewUsers[0, 0]; }
public FormAdminRetireUser(BusinessLayer.UserInformation userInformation) { InitializeComponent(); //Set window title. this.Text = "Retire User"; this.userInformation = userInformation; textBoxUserID.Text = userInformation.UserID.ToString(); textBoxUsername.Text = userInformation.Username; textBoxRole.Text = userInformation.Role; //Set initial values of datetime pickers. if (userInformation.DateRetired != null) { datePicker.Value = userInformation.DateRetired.GetValueOrDefault().Date; timePicker.Value = userInformation.DateRetired.GetValueOrDefault(); } else { datePicker.Value = DateTime.Now.Date; timePicker.Value = DateTime.Today + new TimeSpan(18, 00, 00); } }
public static int InsertUserInformation(BusinessLayer.UserInformation userInfo) { // They are not handled by CLR Garbage Collector, so using three using int result = -1; String sql = ""; using (SqlConnection cnn = GetSqlConnection()) { sql = "Insert into UserInformation(FirstName, Lastname, Address, City, Province, PostalCode, Country) " + $"values ('{userInfo.FirstName}', '{userInfo.LastName}', '{userInfo.Address}', '{userInfo.City}', '{userInfo.Province}', '{userInfo.PostalCode}', '{userInfo.Country}')"; using (SqlCommand command = new SqlCommand(sql, cnn)) { using (SqlDataAdapter adapter = new SqlDataAdapter()) { cnn.Open(); adapter.InsertCommand = new SqlCommand(sql, cnn); result = adapter.InsertCommand.ExecuteNonQuery(); // 1 == OK } } // command.Dispose(); } return(result); }
private void buttonLogin_Click(object sender, EventArgs e) { string username = textBoxUsername.Text; string password = textBoxPassword.Text; BusinessLayer.UserInformation userInfo = BusinessLayer.LoginFacade.GetUser(username, password); if (userInfo != null) { if (userInfo.DateRetired != null && userInfo.DateRetired < DateTime.Now) { //User is retired. labelIncorrectLogin.Text = "Specified account has been retired"; labelIncorrectLogin.Visible = true; } else { labelIncorrectLogin.Visible = false; switch (userInfo.Role) { case "DOC": this.Hide(); var doctorForm = new FormDoctor(userInfo.UserID); doctorForm.FormClosed += (s, args) => this.Close(); doctorForm.Show(); break; case "REC": //Hide this form. this.Hide(); //Create a new receptionistForm. MainForm ONLY TEMP. var receptionistForm = new FormReceptionist(userInfo.UserID); //Add closing loginForm to the closing event of receptionistForm. receptionistForm.FormClosed += (s, args) => this.Close(); //Show the new receptionistForm. receptionistForm.Show(); break; case "TEC": this.Hide(); var technicianForm = new FormLabTechnician(userInfo.UserID); technicianForm.FormClosed += (s, args) => this.Close(); technicianForm.Show(); break; case "MAN": this.Hide(); var managerForm = new FormLabManager(userInfo.UserID); managerForm.FormClosed += (s, args) => this.Close(); managerForm.Show(); break; case "ADM": this.Hide(); var adminForm = new FormAdmin(); adminForm.FormClosed += (s, args) => this.Close(); adminForm.Show(); break; } } } else { //Incorrect login labelIncorrectLogin.Text = "Username or password is incorrect"; labelIncorrectLogin.Visible = true; } }