private void btnSave_Click(object sender, EventArgs e) { try { if (validateFields()) { int SerialNo = Convert.ToInt32(txtSerialNo.Text); string Mr = txtMr.Text; DateTime Date = Convert.ToDateTime(dtpDate.Text); string FName = txtFName.Text; string Orakh = cmbOrakh.Text; decimal Amount = Convert.ToDecimal(txtAmount.Text); //string FName = txtFName.Text; //string Orakh = cmbOrakh.Text; int Account = 0; if (cmbAccount.SelectedValue != null) { Account = Convert.ToInt32(cmbAccount.SelectedValue.ToString()); } string ContactNo = txtContactNo.Text; int FCardNo = 0; if (!(Int32.TryParse(txtFCardNo.Text, out FCardNo))) { FCardNo = 0; } bool Temp = false; if (chkTemp.Checked) { Temp = true; } else { Temp = false; } int rSuccess = 0; int rAdded = 0; if (mode == 0) { if (!checkSerialNo(SerialNo)) { for (int x = 0; x < dgvHallBooking.RowCount - 1; x++) { int Event = Convert.ToInt32(dgvHallBooking.Rows[x].Cells[0].Value.ToString()); for (int y = 1; y <= 5; y++) { if (dgvHallBooking.Rows[x].Cells[y].Value == null) { dgvHallBooking.Rows[x].Cells[y].Value = "0"; } } if (dgvHallBooking.Rows[x].Cells["Remarks"].Value == null) { dgvHallBooking.Rows[x].Cells["Remarks"].Value = ""; } int Hall1 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall1"].Value.ToString()); int Hall2 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall2"].Value.ToString()); int Hall3 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall3"].Value.ToString()); int Hall4 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall4"].Value.ToString()); int Hall5 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall5"].Value.ToString()); DateTime Dated = Convert.ToDateTime(dgvHallBooking.Rows[x].Cells["Date"].Value.ToString()); string ft = dgvHallBooking.Rows[x].Cells["FTime"].Value.ToString(); string[] fti = ft.Split(Convert.ToChar("-")); int ftime = Convert.ToInt32(fti[0]); DateTime FromTime = Convert.ToDateTime("01/01/2010 " + ftime.ToString() + ":00:00 " + fti[1].ToString()); string tt = dgvHallBooking.Rows[x].Cells["To"].Value.ToString(); string[] tti = tt.Split(Convert.ToChar("-")); int ttime = Convert.ToInt32(tti[0]); DateTime ToTime = Convert.ToDateTime("01/01/2010 " + ttime.ToString() + ":00:00 " + tti[1].ToString()); string Remarks = dgvHallBooking.Rows[x].Cells["Remarks"].Value.ToString(); string Reason = dgvHallBooking.Rows[x].Cells["ReasonCancel"].Value.ToString(); //string FName = txtFName.Text; //string Orakh = cmbOrakh.Text; //decimal Amount = Convert.ToDecimal(txtAmount.Text); bool result = dbLayer.AddHallBooking(SerialNo, Date, Mr, Event, Hall1, Hall2, Hall3, Hall4, Hall5, Dated, Dated, FromTime, ToTime, Remarks, FName, Orakh, Amount, Reason, Account, ContactNo, FCardNo.ToString("00000"), Temp); if (result) { rSuccess++; } } MessageBox.Show(rSuccess.ToString() + " Out Of " + Convert.ToString(dgvHallBooking.RowCount - 1) + " Row(s) Added Successfully!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); btnReset_Click(sender, e); } else { MessageBox.Show("That serial no already exists!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else if (mode == 1) { for (int x = 0; x < dgvHallBooking.RowCount; x++) { int Event = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Event"].Value.ToString()); for (int y = 1; y <= 5; y++) { if (dgvHallBooking.Rows[x].Cells[y].Value == null) { dgvHallBooking.Rows[x].Cells[y].Value = "0"; } } if (dgvHallBooking.Rows[x].Cells["Remarks"].Value == null) { dgvHallBooking.Rows[x].Cells["Remarks"].Value = ""; } int Hall1 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall1"].Value.ToString()); int Hall2 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall2"].Value.ToString()); int Hall3 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall3"].Value.ToString()); int Hall4 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall4"].Value.ToString()); int Hall5 = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["Hall5"].Value.ToString()); DateTime Dated = Convert.ToDateTime(dgvHallBooking.Rows[x].Cells["Date"].Value.ToString()); string ft = dgvHallBooking.Rows[x].Cells["Ftime"].Value.ToString(); string[] fti = ft.Split(Convert.ToChar("-")); int ftime = Convert.ToInt32(fti[0]); DateTime FromTime = Convert.ToDateTime("01/01/2010 " + ftime.ToString() + ":00:00 " + fti[1].ToString()); string tt = dgvHallBooking.Rows[x].Cells["To"].Value.ToString(); string[] tti = tt.Split(Convert.ToChar("-")); int ttime = Convert.ToInt32(tti[0]); DateTime ToTime = Convert.ToDateTime("01/01/2010 " + ttime.ToString() + ":00:00 " + tti[1].ToString()); string Remarks = dgvHallBooking.Rows[x].Cells["Remarks"].Value.ToString(); string Reason = dgvHallBooking.Rows[x].Cells["ReasonCancel"].Value.ToString(); bool result = false; bool added = false; if (dgvHallBooking.Rows[x].Cells["ID"].Value != null) { int rID = Convert.ToInt32(dgvHallBooking.Rows[x].Cells["ID"].Value.ToString()); result = dbLayer.UpdateHallBooking(SerialNo, Date, Mr, Event, Hall1, Hall2, Hall3, Hall4, Hall5, Dated, Dated, FromTime, ToTime, Remarks, rID, FName, Orakh, Amount, Reason, Account, ContactNo, FCardNo.ToString("00000"), Temp); added = false; } else { result = dbLayer.AddHallBooking(SerialNo, Date, Mr, Event, Hall1, Hall2, Hall3, Hall4, Hall5, Dated, Dated, FromTime, ToTime, Remarks, FName, Orakh, Amount, Reason, Account, ContactNo, FCardNo.ToString("00000"), Temp); added = true; } if (result) { if (!added) { rSuccess++; } else { rAdded++; } } } string msg = rSuccess.ToString() + " Out Of " + Convert.ToString(dgvHallBooking.RowCount - rAdded) + " Record(s) Updated Successfully!"; if (rAdded != 0) { msg += "\n\n" + rAdded.ToString() + " new Records added!"; } tblTransactionsTableAdapter.InsertTransaction(2, "HBK", Convert.ToInt32(SerialNo), DateTime.Today, Convert.ToDecimal(txtDeduction.Text), 0, Mr + " " + FName + " " + Orakh + "", "", "Serial No = " + SerialNo.ToString("00000")); MessageBox.Show(msg, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); //txtSerialNo.ReadOnly = false; //btnReset_Click(sender, e); mode = 0; btnCloseView.Visible = true; btnLoad.Visible = false; btnSave.Enabled = false; btnReset.Enabled = false; btnEdit.Visible = true; btnDelete.Visible = true; this.AcceptButton = btnEdit; dgvHallBooking.ReadOnly = true; dgvHallBooking.AllowUserToDeleteRows = false; //dtpDate.Enabled = false; txtMr.ReadOnly = true; btnCloseView_Click(sender, e); txtSerialNo.ReadOnly = false; txtSerialNo.Focus(); } } } catch (FormatException) { MessageBox.Show("Please enter valid values in their respective format!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }