private void btn_Delete_Click(object sender, EventArgs e) { foreach (DataGridViewRow dgvr in this.dgv_Show.SelectedRows) { int selectedIndex = dgv_Show.SelectedRows[0].Index; int rowID = 0; DialogResult result; result = MessageBox.Show("Are you sure you want to delete this record?", "Delete record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == System.Windows.Forms.DialogResult.Yes) { try { rowID = int.Parse(dgv_Show[0, selectedIndex].Value.ToString()); cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "DELETE FROM " + department + " WHERE ID=" + rowID + ""; cmd.Connection = dbh.GetCon(); dbh.OpenCon(); try { cmd.ExecuteNonQuery(); } catch { SqlCommand cmd2 = new SqlCommand(); cmd2.CommandType = CommandType.Text; cmd2.CommandText = "UPDATE " + ConvertDepartment() + " SET " + ConvertSqlSet() + " WHERE " + ConvertSqlWhere(rowID) + ""; cmd2.Connection = dbh.GetCon(); cmd2.ExecuteNonQuery(); cmd.ExecuteNonQuery(); } dbh.FillDataGridView(dgv_Show, "SELECT * FROM " + department); dbh.CloseCon(); MessageBox.Show("Delete was succesful!", "Succes!"); } catch { MessageBox.Show("Can't delete this record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
//Fills combobox with IDs and Names from the table defined in the custom constructor. private void frm_Edit_Load(object sender, EventArgs e) { this.StartPosition = FormStartPosition.CenterScreen; cmd = new SqlCommand("SELECT ID,Name FROM " + table + "", dbh.GetCon()); SqlDataReader reader; dbh.OpenCon(); reader = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(string)); dt.Columns.Add("Name", typeof(string)); dt.Load(reader); cb_Customers.ValueMember = "ID"; cb_Customers.DisplayMember = "Name"; cb_Customers.DataSource = dt; dbh.CloseCon(); if (id != null) { cb_Customers.SelectedValue = id; } reader.Dispose(); }
private string[] getColumnsName() { List <string> listacolumnas = new List <string>(); using (SqlCommand command = dbh.GetCon().CreateCommand()) { command.CommandText = "select c.name from sys.columns c inner join sys.tables t on t.object_id = c.object_id and t.name = '" + table + "' and t.type = 'U'"; dbh.OpenCon(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { listacolumnas.Add(reader.GetString(0)); } } dbh.CloseCon(); } return(listacolumnas.ToArray()); }
public frm_Add() { InitializeComponent(); dbh = new SQLDatabaseHandler(); sqlCmd = new SqlCommand("Select top 1* from Tbl_Customer", dbh.GetCon()); SqlDataReader sqlDR = sqlCmd.ExecuteReader(); columns = new List <string>(); for (int i = 1; i < sqlDR.FieldCount; i++) { columns.Add(sqlDR.GetName(i)); } sqlDR.Close(); }
public void LogIn(string user, string pass) { user = tb_Username.Text; pass = tb_Password.Text; string query = "SELECT * FROM TBL_LOGIN WHERE USERNAME = @USERNAME AND PASSWORD = @PASSWORD"; dbh.OpenCon(); SqlCommand com = new SqlCommand(query, dbh.GetCon()); com.Parameters.Add(new SqlParameter("@USERNAME", user)); com.Parameters.Add(new SqlParameter("@PASSWORD", pass)); SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) { uN = reader.GetString(0); pW = reader.GetString(1); } if (uN == user && pW == pass) { MessageBox.Show("Login Successful.", "Succes!"); switch (uN) { case "Admin": permissions = 1; break; case "Sales": permissions = 2; break; case "Development": permissions = 4; break; case "Finance": permissions = 3; break; default: MessageBox.Show("Er is iets fout gegaan bij 'IsLoggedIn()'"); permissions = 0; break; } Form frmMain = new frm_Main(permissions); frmMain.StartPosition = FormStartPosition.CenterScreen; Program.setForm(frmMain); this.Close(); } else if (uN != user || pW != pass) { MessageBox.Show("Invalid Username and/or Password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } dbh.CloseCon(); }