Hi friends i m developing attendance percentage module in student automation system.Percentage will be calculated based on monthly wise.here i m using foreach statement.if i selects section "A"(contains 5 no of students) means all student attendance percentage will be displayed on grid view.the percentage calculation are done to first name students.my problem is the same percentage are repeated to all student..can anyone correct my problem......Thanks in advance..
here with i had attached the sample piece of code..
Loading

Terrance CPosted Oct 10, 2013, 11:29 AM
selva kumarPosted Oct 10, 2013, 8:33 AM
public partial class Attendance_Percentage : System.Web.UI.Page
{
SqlConnection Con;
SqlCommand Cmd;
SqlDataAdapter Sdap;
protected void Page_Load(object sender, EventArgs e)
{
Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=studentmgmt;Integrated Security=True");
if (!IsPostBack)
{
ddlbind1();
DropDownList2.Items.Add("<-Select->");
DropDownList3.Items.Add("<-Select->");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
gv2_bind();
foreach (GridViewRow row in GridView2.Rows)
{
lblmonth.Text = DropDownList3.SelectedItem.Text;
noofdayspresent();
noofdaysabsent();
Label lbname = (Label)row.FindControl("lblname");
Label lbclass = (Label)row.FindControl("lblclass");
Label lbsection = (Label)row.FindControl("lblsection");
totalworkingdays();
double present = Convert.ToDouble(lblnoofdayspresent.Text);
double X = 100;
double totaldays = Convert.ToDouble(lbltotalworkingdays.Text);
double total = (present * X) / totaldays;
lblpercentage.Text = Convert.ToString(total) + "%";
Con.Open();
SqlCommand Cmd2 = new SqlCommand("Insert Into dailyatttot values ('" + lbname.Text + "','" + lbclass.Text + "','" + lbsection.Text + "','" + lblmonth.Text + "','" + lbltotalworkingdays.Text + "','" + lblnoofdayspresent.Text + "','" + lblnoofdaysabsent.Text + "','" + lblpercentage.Text + "')", Con);
Cmd2.ExecuteNonQuery();
Con.Close();
gv1_bind();
}
}
catch (Exception Ex)
{
Response.Write(Ex.ToString());
}
finally
{
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
ddlbind2();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
ddlbind3();
}
public void ddlbind1()
{
try
{
Con.Open();
Cmd = new SqlCommand("Select Distinct Class From student",Con);
SqlDataReader Sdr;
Sdr = Cmd.ExecuteReader();
DropDownList1.Items.Add("<-Select->");
while (Sdr.Read())
{
DropDownList1.Items.Add(Sdr["Class"].ToString());
}
}
catch (Exception Ex1)
{
Label labelex = new Label();
labelex.Text = Ex1.ToString();
}
finally
{
Con.Close();
}
}
public void ddlbind2()
{
try
{
Con.Open();
Cmd = new SqlCommand("Select Distinct Section From student",Con);
SqlDataReader Sdr;
Sdr = Cmd.ExecuteReader();
DropDownList2.Items.Add("<-Select->");
while (Sdr.Read())
{
DropDownList2.Items.Add(Sdr["Section"].ToString());
}
}
catch (Exception Ex2)
{
Label labelex1 = new Label();
labelex1.Text = Ex2.ToString();
}
finally
{
Con.Close();
}
}
public void ddlbind3()
{
try
{
Con.Open();
Cmd = new SqlCommand("Select Distinct [Month] From Dailyattendance",Con);
SqlDataReader Sdr;
Sdr = Cmd.ExecuteReader();
DropDownList3.Items.Add("<-Select->");
while (Sdr.Read())
{
DropDownList3.Items.Add(Sdr["Month"].ToString());
}
}
catch (Exception Ex3)
{
Label labelex2 = new Label();
labelex2.Text = Ex3.ToString();
}
finally
{
Con.Close();
}
}
public void gv1_bind()
{
try
{
Con.Open();
Sdap = new SqlDataAdapter("select [Name],Class,Section,[Month],Total_Workingdays,No_ofdaysPresent,No_ofdaysAbsent,Percentage From dailyatttot Where Section='" + DropDownList2.SelectedItem.Text + "'", Con);
DataSet Ds = new DataSet();
Sdap.Fill(Ds);
GridView1.DataSource = Ds;
GridView1.DataBind();
}
catch (Exception Exgv1)
{
Label labelgv1 = new Label();
labelgv1.Text = Exgv1.ToString();
}
finally
{
Con.Close();
}
}
public void gv2_bind()
{
try
{
Con.Open();
Sdap = new SqlDataAdapter("select T_ID,[Name],Class,Section From Dailyattendance Where Class='"+DropDownList1.SelectedItem.Text+"' and Section='"+DropDownList2.SelectedItem.Text+"'", Con);
DataSet Ds1 = new DataSet();
Sdap.Fill(Ds1);
GridView2.DataSource = Ds1;
GridView2.DataBind();
}
catch (Exception Exgv2)
{
Label labelgv2 = new Label();
labelgv2.Text = Exgv2.ToString();
}
finally
{
Con.Close();
}
}
public void totalworkingdays()
{
try
{
foreach (GridViewRow row in GridView2.Rows)
{
Label lbname = (Label)row.FindControl("lblname");
Con.Open();
Sdap = new SqlDataAdapter("Select (count(Month)/2) Totalworkingdays from Dailyattendance Where Month='" + DropDownList3.SelectedItem.Text + "'", Con);
DataSet ds = new DataSet();
Sdap.Fill(ds);
lbltotalworkingdays.Text = ds.Tables[0].Rows[0]["Totalworkingdays"].ToString();
Con.Close();
double a = Convert.ToDouble(lbltotalworkingdays.Text);
//double tot = a / 2;
lbltotalworkingdays.Text = Convert.ToString(a);
}
}
catch (Exception EXworkdays)
{
Label labelwork = new Label();
labelwork.Text = EXworkdays.ToString();
}
finally
{
}
}
public void noofdayspresent()
{
try
{
foreach (GridViewRow row in GridView2.Rows)
{
Label lbname = (Label)row.FindControl("lblname");
Con.Open();
Sdap = new SqlDataAdapter("Select count(Present) no_of_dayspresent from Dailyattendance where [Name]='" + lbname.Text + "' and Month='" + DropDownList3.SelectedItem.Text + "' and Present='Present' and (Session1='Forenoon' or Session1='Afternoon')", Con);
DataSet ds = new DataSet();
Sdap.Fill(ds);
lblnoofdayspresent.Text = ds.Tables[0].Rows[0]["no_of_dayspresent"].ToString();
Con.Close();
double b = Convert.ToDouble(lblnoofdayspresent.Text);
double tot1 = b / 2;
lblnoofdayspresent.Text = Convert.ToString(tot1);
}
}
catch (Exception EXpresentdays)
{
Label labelpresent = new Label();
labelpresent.Text = EXpresentdays.ToString();
}
finally
{
}
}
public void noofdaysabsent()
{
try
{
foreach (GridViewRow row in GridView2.Rows)
{
Label lbname = (Label)row.FindControl("lblname");
Con.Open();
Sdap = new SqlDataAdapter("Select count([Absent]) no_of_dayabsent from Dailyattendance where [Name]='" + lbname.Text + "' and Month='" + DropDownList3.SelectedItem.Text + "' and [Absent]='Absent' and (Session1='Forenoon' or Session1='Afternoon')", Con);
DataSet ds = new DataSet();
Sdap.Fill(ds);
lblnoofdaysabsent.Text = ds.Tables[0].Rows[0]["no_of_dayabsent"].ToString();
Con.Close();
double c = Convert.ToDouble(lblnoofdaysabsent.Text);
double tot2 = c / 2;
lblnoofdaysabsent.Text = Convert.ToString(tot2);
}
}
catch (Exception EXabsentdays)
{
Label labelabsent = new Label();
labelabsent.Text = EXabsentdays.ToString();
}
finally
{
}
}
}
Terrance CPosted Oct 10, 2013, 8:17 AM