public static bool DeleteAccount(RequestAccounts request) { using (SqlConnection con = new SqlConnection(connectionstring)) { using (SqlCommand proc = new SqlCommand("spDeleteAccount", con) { CommandType = CommandType.StoredProcedure }) { proc.Parameters.Add("@id", SqlDbType.Int).Value = request.Id; try { con.Open(); Console.WriteLine("Connection Success!"); try { proc.ExecuteNonQuery(); // this is where I run my stored procedure } catch (Exception e) { Console.WriteLine("-------------"); Console.WriteLine("Procedure Error on spAddAccount : {0}", e.ToString()); Console.WriteLine("-------------"); return(false); } return(true); } catch (SqlException) { Console.WriteLine("Connection Failure!"); return(false); } } } }
public static bool AddAccount(RequestAccounts request) { using (SqlConnection con = new SqlConnection(connectionstring)) { using (SqlCommand proc = new SqlCommand("spAddAccount", con) { CommandType = CommandType.StoredProcedure }) { proc.Parameters.Add("@company_id", SqlDbType.Int).Value = request.CompanyId; proc.Parameters.Add("@username", SqlDbType.NVarChar).Value = request.Username; proc.Parameters.Add("@password", SqlDbType.NVarChar).Value = Security.Encrypt(request.Password, SALT); try { con.Open(); Console.WriteLine("Connection Success!"); try { proc.ExecuteNonQuery(); // this is where I run my stored procedure } catch (Exception e) { Console.WriteLine("-------------"); Console.WriteLine("Procedure Error on spAddAccount : {0}", e.ToString()); Console.WriteLine("-------------"); return(false); } return(true); } catch (SqlException) { Console.WriteLine("Connection Failure!"); return(false); } } } }
private void deleteAccountButton_Click(object sender, EventArgs e) { int AccountDeleteId; if (accountsGrid.SelectedRows.Count > 0) { using (var form = new Confirm()) { var confirmDelete = form.ShowDialog(); string delete = confirmDelete.ToString(); if (delete == "OK") { for (int i = 0; i < accountsGrid.SelectedRows.Count; i++) { DataGridViewRow row = accountsGrid.SelectedRows[i]; AccountDeleteId = Convert.ToInt32(row.Cells["Id"].Value); RequestAccounts request = new RequestAccounts(CompanySelected, AccountDeleteId); Connect.DeleteAccount(request); BindDataGrid(); break; } } } } else { MessageBox.Show("No row selected. Please, make sure there are rows to be selected."); } }
private void addAccountFormButton_Click(object sender, EventArgs e) { string username = userTextBox.Text; string password = passTextBox.Text; if (String.IsNullOrEmpty(username)) { MessageBox.Show("Please, insert a valid username."); return; } else if (String.IsNullOrEmpty(password)) { MessageBox.Show("Please, insert a valid password."); return; } RequestAccounts account = new RequestAccounts(SelectedCompany, username, password); bool addOk = false; try { addOk = Connect.AddAccount(account); } catch (Exception err) { MessageBox.Show("An error occured when adding the account : {0}", err.ToString()); } if (addOk) { this.Close(); } else { MessageBox.Show("An error occured when adding the account! Please, try again later."); } }
private void BindDataGrid() { RequestAccounts companyAccounts = new RequestAccounts(CompanySelected); List <RequestAccounts> Accounts = Connect.ListAccounts(companyAccounts); accountsGrid.DataSource = Accounts; accountsGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; accountsGrid.Columns["Id"].Visible = false; accountsGrid.Columns["CompanyId"].Visible = false; }
public static List <RequestAccounts> ListAccounts(RequestAccounts request) { List <RequestAccounts> response = new List <RequestAccounts>(); using (SqlConnection con = new SqlConnection(connectionstring)) { using (SqlCommand proc = new SqlCommand("spListAccount", con) { CommandType = CommandType.StoredProcedure }) { proc.Parameters.Add("@company_id", SqlDbType.Int).Value = request.CompanyId; try { con.Open(); Console.WriteLine("Connection Success!"); SqlDataReader dr = proc.ExecuteReader(); while (dr.Read()) { int dbAccountId = Convert.ToInt32(dr["Id"]); int dbAccountCompanyId = Convert.ToInt32(dr["CompanyId"]); string dbAccountUsername = Convert.ToString(dr["Username"]); string dbAccountPassword = Security.Decrypt(Convert.ToString(dr["Password"]), SALT); Console.WriteLine("----------------"); Console.WriteLine("Account Id : {0}", dbAccountId); Console.WriteLine("Account Company Id : {0}", dbAccountCompanyId); Console.WriteLine("Account username : {0}", dbAccountUsername); Console.WriteLine("Account password decrypted : {0}", dbAccountPassword); Console.WriteLine("Account password encrypted : {0}", Convert.ToString(dr["Password"])); Console.WriteLine("----------------"); RequestAccounts account = new RequestAccounts(dbAccountId, dbAccountCompanyId, dbAccountUsername, dbAccountPassword); response.Add(account); } return(response); } catch (SqlException) { Console.WriteLine("Connection Failure!"); return(response); } } } }