Hi akhter,
Change the procedure with below to get the txtpresent and txtabsent column.
SQL
ALTER PROCEDURE [dbo].[Sp_Employee_Detail]
AS
BEGIN
SELECT Emp_ID, EMp_Name, EMP_Salary, Emp_Deduction,0 'txtpresent', 0 'txtabsent'
FROM tbl_Employee_Master
END
Change with the below code to update the DataTable.
Code
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtentrydate.Text = DateTime.Now.ToString();
txtmonth.Text = DateTime.Now.Month.ToString();
Loadidlabel();
loademployee();
}
}
private void loademployee()
{
con.Open();
SqlCommand cmdf = new SqlCommand("Sp_Employee_Detail");
cmdf.Connection = con;
cmdf.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmdf))
{
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
this.gvv.DataSource = dt;
this.gvv.DataBind();
ViewState["dt"] = dt;
}
cmdf.Dispose();
con.Close();
}
}
private void Loadidlabel()
{
con.Open();
SqlCommand cmd = new SqlCommand("Sp_Lastinsert_salary_ID");
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
lblastid.Text = dt.Rows[0][0].ToString();
}
else
{
//Nothing returned
lblastid.Text = "No Record";
}
con.Close();
}
protected void OnCalculate(object sender, EventArgs e)
{
TextBox present = sender as TextBox;
GridViewRow row = present.NamingContainer as GridViewRow;
Label absent = row.FindControl("txtabsent") as Label;
Label gsalary = row.FindControl("EMP_Salary") as Label;
Label salary = row.FindControl("txtsalary") as Label;
Calculate(txttotalday, present, absent, gsalary, salary);
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows[row.RowIndex]["txtpresent"] = present.Text.Trim();
dt.Rows[row.RowIndex]["txtabsent"] = absent.Text.Trim();
ViewState["dt"] = dt;
}
private void Calculate(TextBox total, TextBox present, Label absent, Label gsalary, Label salary)
{
salary.Text = ((Convert.ToDecimal(gsalary.Text) / Convert.ToDecimal(total.Text) * Convert.ToDecimal(present.Text)).ToString());
absent.Text = (Convert.ToDecimal(total.Text) - Convert.ToDecimal(present.Text)).ToString();
salary.Text = Math.Round(double.Parse(salary.Text), 0).ToString();
}
protected void btn_gen_Click(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand("SP_insert_Salary_master", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Sal_ID", lblastid.Text);
cmd.Parameters.AddWithValue("@Sal_Date", txtentrydate.Text);
cmd.Parameters.AddWithValue("@Sal_Month", txtmonth.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DataTable dt = (DataTable)ViewState["dt"];
string id, salary, deduction, present, absent;
int ResultValue = 0;
foreach (DataRow row in dt.Rows)
{
id = (row["Emp_ID"].ToString());
salary = (row["Emp_Salary"].ToString());
deduction = (row["Emp_Deduction"].ToString());
present = (row["txtpresent"].ToString());
absent = (row["txtabsent"].ToString());
ResultValue += this.InsertRows(id, salary, deduction, present, absent);
}
if (ResultValue == dt.Rows.Count)
{
// Response.Redirect("impprnt.aspx?impno=" + lblast.Text + "");
}
else
{
string Url = "impprnt.aspx?I_ID=" + lblastid.Text;
Response.Write("<script language='javascript'>window.open('" + Url + "','_blank','');");
Response.Write("</script>");
//Response.Redirect("lmpfrm.aspx");
}
}
}
private int InsertRows(string id, string salary, string deduction, string present, string absent)
{
using (SqlCommand cmd = new SqlCommand("Sp_Insert_Salary_Detail", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@emp_id", id);
cmd.Parameters.AddWithValue("@Sal_ID", lblastid.Text);
cmd.Parameters.AddWithValue("@emp_salary", salary);
cmd.Parameters.AddWithValue("@emp_deduction", deduction);
cmd.Parameters.AddWithValue("@emp_present", present);
cmd.Parameters.AddWithValue("@emp_absent", absent);
con.Open();
int ResultValue = cmd.ExecuteNonQuery();
con.Close();
return ResultValue;
}
}