private void btnUpdate_Click(object sender, EventArgs e)
{
ep.Clear();
try
{
int selectedRowIndex = dgvFacultyLeaveList.CurrentRow.Index;
string facultyID = txtID.Text.Trim();
string leaveType = cboLeaveType.Text.Trim();
string totalBalance = txttotalleave.Text.Trim();
string month = cboMonth.Text;
string noOfDays = NoOfDays.Text;
string leaveDate = dtpLeavedate.Value.ToString("yyyy-MM-dd");
string leaveTime = dtpLeaveTime.Value.ToString("HH:mm:ss");
string endDate = dtpEndDate.Value.ToString("yyyy-MM-dd");
string endTime = dtpEndTime.Value.ToString("HH:mm:ss");
string sanctionedLeave = txtLSanctioned.Text;
string availedLeave = txtLAvailed.Text.Trim();
string balance = txtBalance.Text;
string reason = txtReasons.Text;
string updateQuery = @"UPDATE Facultyleave SET
Faculty_ID = @Faculty_ID,
LeaveType = @LeaveType,
TotalBalance = @TotalBalance,
Month = @Month,
Days = @Days,
LeaveDate = @LeaveDate,
LeaveTime = @LeaveTime,
EndDate = @EndDate,
EndTime = @EndTime,
SancationedLeave = @SancationedLeave,
AvailedLeave = @AvailedLeave,
Balance = @Balance,
Reason = @Reason
WHERE Faculty_ID = @Faculty_ID
AND LeaveID = (SELECT MAX(LeaveID) FROM FacultyLeave WHERE Faculty_ID = @Faculty_ID);";
using (var conn = DatabaseAccess.ConnOpen())
{
using (var cmd = new SqlCommand(updateQuery, conn))
{
cmd.Parameters.AddWithValue("@Faculty_ID", facultyID);
cmd.Parameters.AddWithValue("@LeaveType", leaveType);
cmd.Parameters.AddWithValue("@TotalBalance", totalBalance);
cmd.Parameters.AddWithValue("@Month", month);
cmd.Parameters.AddWithValue("@Days", noOfDays);
cmd.Parameters.AddWithValue("@LeaveDate", leaveDate);
cmd.Parameters.AddWithValue("@LeaveTime", leaveTime);
cmd.Parameters.AddWithValue("@EndDate", endDate);
cmd.Parameters.AddWithValue("@EndTime", endTime);
cmd.Parameters.AddWithValue("@SancationedLeave", sanctionedLeave);
cmd.Parameters.AddWithValue("@AvailedLeave", availedLeave);
cmd.Parameters.AddWithValue("@Balance", balance);
cmd.Parameters.AddWithValue("@Reason", reason);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
int updatedLeaveID = Convert.ToInt32(cmd.ExecuteScalar());
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[0].Value = updatedLeaveID;
int originalTotalBalance = Convert.ToInt32(totalBalance);
int originalAvailedLeave = Convert.ToInt32(txtLAvailed.Text);
int updatedNoOfDays = Convert.ToInt32(noOfDays);
int updatedBalance = originalTotalBalance - updatedNoOfDays;
int updatedAvailedLeave = originalAvailedLeave + updatedNoOfDays;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[0].Value = updatedLeaveID;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[1].Value = facultyID;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[2].Value = leaveType;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[3].Value = updatedBalance;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[4].Value = month;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[5].Value = updatedNoOfDays;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[6].Value = leaveDate;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[7].Value = leaveTime;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[8].Value = endDate;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[9].Value = endTime;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[10].Value = sanctionedLeave;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[10].Value = sanctionedLeave;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[11].Value = updatedAvailedLeave;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[12].Value = balance;
dgvFacultyLeaveList.Rows[selectedRowIndex].Cells[13].Value = reason;
dgvFacultyLeaveList.Refresh();
txtID.Text = "";
cboLeaveType.SelectedIndex = -1;
txttotalleave.Text = "";
cboMonth.SelectedIndex = -1;
NoOfDays.Text = "";
dtpLeavedate.Value = DateTime.Now;
dtpLeaveTime.Value = DateTime.Now;
dtpEndDate.Value = DateTime.Now;
dtpEndTime.Value = DateTime.Now;
txtLSanctioned.Text = "";
txtLAvailed.Text = "";
txtBalance.Text = "";
txtReasons.Text = "";
MessageBox.Show("Record updated successfully!");
EnableComponents();
FillGrid("");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error updating the record: " + ex.Message);
} }
What I have tried:
this is the code of my update button for Leave Module i have problem that when i want to update the leave for staff member so it calculate the previous value which is in NoOfDays textbox suppose i add leave for staff member the total balance = 10 . i add 1 leave than remaining balance =9, availed =1. so than i want to update the leave so i edit it and i remove the value which is in the textbox of NoOfDay which is =1 and write 2 so it do like add 1+2 =3 and than make balance =7, availed =3. the expected outcome should be Remaining balance = 8, availed = 2. my NoOfDays.text textbox take the previous value i add a button also that make make it 0 but its also calculate the zero when i click on the remove button to make noofdays textbox 0 it again add one value like balance =9, availed =1 when i remove the value from noofdays textbox so its become zero than it make balance = 8, availed = 2.