public override void AddData() { FormEditOrganizatation frm = new FormEditOrganizatation(); DialogResult res = frm.ShowDialog(); if (res == DialogResult.OK) { string sql = @"INSERT INTO organizations (name, chief_name, secretary_name, staff) VALUES (@name, @chief_name, @secretary_name, @staff)"; MySqlCommand cmd = new MySqlCommand(sql, m_connection); cmd.Parameters.AddWithValue("@name", frm.orgName); cmd.Parameters.AddWithValue("@chief_name", frm.orgChiefName); cmd.Parameters.AddWithValue("@secretary_name", frm.orgSecretaryName); cmd.Parameters.AddWithValue("@staff", frm.orgStaff); int rows_affected = cmd.ExecuteNonQuery(); RefreshData(cmd.LastInsertedId); } }
private void btAddOrg_Click(object sender, EventArgs e) { FormEditOrganizatation frm = new FormEditOrganizatation(); DialogResult res = frm.ShowDialog(); if (res == DialogResult.OK) { string sql = "INSERT INTO organizations (name, chief_name, secretary_name) VALUES (@name, @chief_name, @secretary_name)"; MySqlCommand cmd = new MySqlCommand(sql, m_connection); cmd.Parameters.AddWithValue("@name", frm.orgName); cmd.Parameters.AddWithValue("@chief_name", frm.orgChiefName); cmd.Parameters.AddWithValue("@secretary_name", frm.orgSecretaryName); int rows = cmd.ExecuteNonQuery(); LoadOrganizations(); this.fld_id_org = cmd.LastInsertedId; } }
public override void EditData() { DataGridViewRow row = dgOrganizations.CurrentRow; long id = Convert.ToInt64(row.Cells["id"].Value); FormEditOrganizatation frm = new FormEditOrganizatation(); try { string sql_select = @"SELECT * FROM organizations WHERE id=@id"; MySqlCommand cmd_select = new MySqlCommand(sql_select, m_connection); cmd_select.Parameters.AddWithValue("@id", id); MySqlDataReader rdr = cmd_select.ExecuteReader(); if (rdr.Read()) { // load values into the form frm.orgName = rdr["name"].ToString(); frm.orgChiefName = rdr["chief_name"].ToString(); frm.orgSecretaryName = rdr["secretary_name"].ToString(); frm.orgStaff = rdr["staff"] is DBNull ? null : Encoding.UTF8.GetString(rdr["staff"] as byte[]); rdr.Close(); DialogResult res = frm.ShowDialog(); if (res == System.Windows.Forms.DialogResult.OK) { string sql_update = @"UPDATE organizations SET name=@name, chief_name=@chief_name, secretary_name=@secretary_name, staff=@staff WHERE id=@id"; MySqlCommand cmd_update = new MySqlCommand(sql_update, m_connection); cmd_update.Parameters.AddWithValue("@id", id); // add values from the form cmd_update.Parameters.AddWithValue("@name", frm.orgName); cmd_update.Parameters.AddWithValue("@chief_name", frm.orgChiefName); cmd_update.Parameters.AddWithValue("@secretary_name", frm.orgSecretaryName); cmd_update.Parameters.AddWithValue("@staff", frm.orgStaff); int rows_affected = cmd_update.ExecuteNonQuery(); RefreshData(id); } } else MessageBox.Show(String.Format("There is no organization with id={0}!", id), "Error"); } catch (MySql.Data.MySqlClient.MySqlException ex) { MessageBox.Show(ex.Message); } frm.Close(); }