private void btnDelete_Click(object sender, EventArgs e) { if (dgvResult.Rows.Count == 0) { MessageBox.Show("No record to delete.", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if ((from DataGridViewRow row in dgvColumns.Rows where Convert.ToBoolean(row.Cells["isPrimaryKeyDataGridViewCheckBoxColumn"].Value) select row).ToList().Count() == 0) { MessageBox.Show("Cannot delete record without primary key", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete rows?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { string dbName = this.GetDBName(); string tableName = this.GetTableName(); Dictionary <string, object> paramByValues = new Dictionary <string, object>(); int count = 0; string whereQuery = "("; foreach (DataGridViewRow row in dgvResult.SelectedRows) { string whereSubQuery = ""; foreach (ColumnDetail col in this.GetColumnDetails(true, row.Index).Where(x => x.IsPrimaryKey)) { string parameterName = string.Format("@{0}{1}", col.ColumnName, count.ToString()); if (whereSubQuery != "") { whereSubQuery = " AND "; } whereSubQuery = string.Format("{0}={1}", col.ColumnName, parameterName); paramByValues.Add(parameterName, col.ColumnValue); } string a = ""; if (whereQuery != "(") { whereQuery = whereQuery + " OR ("; } whereQuery = whereQuery + whereSubQuery + ")"; count++; } string deleteQuery = string.Format("use {0};DELETE FROM {1} WHERE {2};", dbName, tableName, whereQuery); try { MSSQLBase.SQLBase s = new MSSQLBase.SQLBase(DBConnection.DbCon.Connection); s.ExecuteNonQuery(deleteQuery, paramByValues); btnExec.PerformClick(); MessageBox.Show(string.Format("{0} row/s deleted", count.ToString(), "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void ExecuteUpdate(string query) { Dictionary <string, object> columnByValues = new Dictionary <string, object>(); foreach (ColumnDetail col in from t in this._ColumnDetails where !t.IsIdentity && !this._TypesNotEditable.Contains(t.DataType) select t) { columnByValues.Add(string.Format("@{0}", col.ColumnName), GetNewValue(col.ColumnName)); } try { MSSQLBase.SQLBase s = new MSSQLBase.SQLBase(DBConnection.DbCon.Connection); s.ExecuteNonQuery(query, columnByValues); MessageBox.Show("Successfully Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information); this.SuccessSave = true; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "btnSave", MessageBoxButtons.OK, MessageBoxIcon.Error); } }