private void btnCancel_Click(object sender, EventArgs e) { this._editVehicle = null; this._operationMode = OperationModesEnum.None; this.panel1.Enabled = false; this.btnSubcintractorChange.Enabled = false; this.btnDriverChange.Enabled = this.btnSubcintractorChange.Enabled; this.btnAddNew.Focus(); this.ClearControls(); }
private void btnEdit_Click(object sender, EventArgs e) { if (this.dgvVehicles.CurrentRow == null) { return; } this.btnSubcintractorChange.Enabled = true; this.btnDriverChange.Enabled = this.btnSubcintractorChange.Enabled; this._editVehicle = this.dSLOGS.Vehicles.Select(this.dSLOGS.Vehicles.Vehicle_IDColumn.ColumnName + "='" + this.dgvVehicles.CurrentRow.Cells[0].Value + "'" )[0] as DSLOGS.VehiclesRow; this._operationMode = OperationModesEnum.Edit; this.RetrieveVehicle(); this.panel1.Enabled = true; this.btnSubcintractorChange.Enabled = true; this.btnDriverChange.Enabled = this.btnSubcintractorChange.Enabled; this.cmbSubcontractors.Focus(); //this.txtSubcontractorName.SelectAll(); }
private void btnSave_Click(object sender, EventArgs e) { if (this.txtRegnNo.Text.Trim() == "") { MessageBox.Show("Please enter registration number", "Error", MessageBoxButtons.OK , MessageBoxIcon.Exclamation); this.txtRegnNo.Focus(); return; } if (this.cmbSubcontractors.SelectedIndex < 0) { MessageBox.Show("Please select a subcontractor", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.cmbSubcontractors.Focus(); return; } if (this.chkDriver.Checked && this.cmbDrivers.SelectedIndex < 0) { MessageBox.Show("Please select a driver", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.cmbDrivers.Focus(); return; } decimal?id = this._operationMode == OperationModesEnum.AddNew ? 0 : this._editVehicle.Vehicle_ID; string name = this.txtRegnNo.Text.Trim(); System.Data.SqlClient.SqlTransaction trans = null; System.Data.SqlClient.SqlConnection con = this.vehiclesTableAdapter.Connection; if (con.State != ConnectionState.Open) { con.Open(); } trans = con.BeginTransaction(); this.vehiclesTableAdapter.Transaction = trans; DSLOGS.VehiclesDataTable dtDuplicateVehicles = this.vehiclesTableAdapter.GetDataByRegistrationNoByNotVehicleID(id, name); if (dtDuplicateVehicles.Rows.Count > 0) { trans.Rollback(); MessageBox.Show($"Another vehicle with registration No '{name}' already exists. Please enter some other registration no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.txtRegnNo.Focus(); this.txtRegnNo.SelectAll(); return; } if (this._operationMode == OperationModesEnum.AddNew) { int x = this.vehiclesTableAdapter.Insert(ref id, (decimal?)this.cmbSubcontractors.SelectedValue , (decimal?)(this.chkDriver.Checked ? this.cmbDrivers.SelectedValue: null), (decimal?)null , this.txtRegnNo.Text.Trim(), this.cmbVehicleType.Text.Trim()); if (x == 1) { DSLOGSTableAdapters.SubcontractorVehiclesTableAdapter taSubcontractorVehicles = new DSLOGSTableAdapters.SubcontractorVehiclesTableAdapter(); taSubcontractorVehicles.Connection = con; taSubcontractorVehicles.Transaction = trans; decimal?subcontractorVehicleID = 0; int y = taSubcontractorVehicles.Insert(ref subcontractorVehicleID, (decimal?)this.cmbSubcontractors.SelectedValue , id, this.dtSubcontractorStartDate.Value, (DateTime?)null, "Initial subcontractor"); decimal?driverID = this.chkDriver.Checked && cmbDrivers.SelectedIndex >= 0 ? (decimal?)this.cmbDrivers.SelectedValue : null; if (driverID != null) { DSLOGSTableAdapters.DriverVehiclesTableAdapter taDriverVehicles = new DSLOGSTableAdapters.DriverVehiclesTableAdapter(); taDriverVehicles.Connection = con; taDriverVehicles.Transaction = trans; decimal?driverVehicleID = 0; int z = taDriverVehicles.Insert(ref driverVehicleID, driverID, id, this.dtDriverrStartDate.Value, null, "Initial driver"); } trans.Commit(); con.Close(); MessageBox.Show("Vehicle data saved successfully", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information); this.ClearControls(); this._operationMode = OperationModesEnum.None; this.panel1.Enabled = false; this.btnSubcintractorChange.Enabled = true; this.btnDriverChange.Enabled = this.btnSubcintractorChange.Enabled; this.RetrieveData(); this.btnAddNew.Focus(); } } else if (this._operationMode == OperationModesEnum.Edit) { decimal?driverid = (decimal?)(this.chkDriver.Checked? this.cmbDrivers.SelectedValue: null); int x = this.vehiclesTableAdapter.Update((decimal?)this.cmbSubcontractors.SelectedValue, driverid, null, name, this.cmbVehicleType.Text.Trim(), id); if (x == 1) { MessageBox.Show("Vehicle data updated successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); this.ClearControls(); this._editVehicle = null; this._operationMode = OperationModesEnum.None; this.panel1.Enabled = false; this.btnSubcintractorChange.Enabled = false; this.btnSubcintractorChange.Enabled = true; this.btnDriverChange.Enabled = this.btnSubcintractorChange.Enabled; this.btnDriverChange.Enabled = this.btnSubcintractorChange.Enabled; this.RetrieveData(); this.btnAddNew.Focus(); } } }