Data was not updating the first time second time it is updating please rectify that problem
//Code for insert and update the data into database
private void btnAdvanceSave_Click(object sender, EventArgs e)
{
if (sanctiondateValidation() == false)
return;
try
{
string str = "Select SrlNo,EmpId,AdvanceType,AmountTaken,SanctionedDate,SanctionedBy from AdvancePaymentsMaster where EmpID='" + txtemployeeid.Text + "'and SanctionedDate=#"+dtpSanctionedDate.Value+"#";
dbConnection conn = new dbConnection();
OleDbDataReader dr = conn.OleDbReaderQuery(str);
if (dr.Read())
{
BOL.bolAdvancePaymentsMaster apm = new PayRoll.BOL.bolAdvancePaymentsMaster();
//apm.AdvanceType = cmbAdvanceType.Text;
apm.AdvanceType = cmbAdvanceType.Text;
apm.AmountTaken = Convert.ToDecimal(txtAmountTaken.Text);
apm.EmpID = txtemployeeid.Text;
apm.SanctionedBy = txtSanctionedBy.Text;
apm.SanctionedDate = Convert.ToDateTime(dtpSanctionedDate.Value);
apm.Update();
MessageBox.Show("Record updated sucessfully","Payroll",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
else
{
BOL.bolAdvancePaymentsMaster apm1 = new PayRoll.BOL.bolAdvancePaymentsMaster();
apm1.AdvanceType = cmbAdvanceType.Text;
apm1.AmountTaken = Convert.ToDecimal(txtAmountTaken.Text);
apm1.EmpID = txtemployeeid.Text;
apm1.SanctionedBy = txtSanctionedBy.Text;
apm1.SanctionedDate = Convert.ToDateTime(dtpSanctionedDate.Value);
apm1.Insert();
MessageBox.Show("Record Successfully Saved","Payroll",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
dr.Close();
loadAdvances();
clearAdvances();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Payroll",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
}
//Code for Load Advances into gridview
private void loadAdvances()
{
gvadvances.DataSource = null;
//string str = "select EmpId as [Employee ID],AdvanceType as [Advance Type],AmountTaken as [Amount],Format(SanctionedDate,"dd-mm-yyyy") as [SDate],SanctionedBy as [Sanctioned By] from [AdvancePaymentsMaster] where EmpID='" + txtemployeeid.Text + "'";
string str = "select EmpId as [Employee ID],AdvanceType as [Advance Type],AmountTaken as [Amount],Format(SanctionedDate,'dd-mm-yyyy') as [SDate],SanctionedBy as [Sanctioned By] from [AdvancePaymentsMaster] where EmpID='" + txtemployeeid.Text + "'";
DataTable dt = conn.executeSelectQueryNonParameter(str);
gvadvances.DataSource = dt;
}
//Code for Bol database
public bool Update(BOL.bolAdvancePaymentsMaster obj)
{
dbConnection conn =new dbConnection();
string sql = "update AdvancePaymentsMaster set AdvanceType=@AdvanceType,AmountTaken=@AmountTaken,SanctionedDate=@SanctionedDate,SanctionedBy=@SanctionedBy where EmpId=@EmpId and SanctionedDate=@SanctionedDate";
OleDbParameter[] oledbParameters = new OleDbParameter[5];
//sqlParameters[0] = new SqlParameter("@Srlno", SqlDbType.Int);
//sqlParameters[0].Value = obj.Srlno;
oledbParameters[0] = new OleDbParameter("@EmpId", OleDbType.VarChar);
oledbParameters[0].Value = obj.EmpID;
oledbParameters[1] = new OleDbParameter("@AdvanceType", OleDbType.VarChar);
oledbParameters[1].Value = obj.AdvanceType;
oledbParameters[2] = new OleDbParameter("@AmountTaken", OleDbType.Decimal);
oledbParameters[2].Value = obj.AmountTaken;
oledbParameters[3] = new OleDbParameter("#@SanctionedDate#", OleDbType.Date);
oledbParameters[3].Value = obj.SanctionedDate;
oledbParameters[4] = new OleDbParameter("@SanctionedBy", OleDbType.VarChar);
oledbParameters[4].Value = obj.SanctionedBy;
return conn.executeUpdateQuery(sql, oledbParameters);
}
//code for insert the database
public bool executeInsertQuery(String _query, OleDbParameter[] sqlParameter)
{
OleDbCommand myCommand = new OleDbCommand();
try
{
myCommand.Connection = openConnection();
myCommand.CommandText = _query;
myCommand.Parameters.AddRange(sqlParameter);
myAdapter.InsertCommand = myCommand;
myCommand.ExecuteNonQuery();
}
catch (OleDbException e)
{
Console.Write("Error - Connection.executeInsertQuery - Query: " + _query + " \nException: \n" + e.StackTrace.ToString());
return false;
}
finally
{
}
return true;
}