Azm Amn

Azm Amn

  • NA
  • 108
  • 17.7k

Adding Label Values as Int

Mar 2 2017 10:44 PM
Hi,
 
I have the below code:
 
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
{
Label1.Text = Session["username"].ToString();
}
con.Open();
SqlCommand cmd = new SqlCommand("SELECT FullName FROM Student WHERE Username= '" + Label1.Text + "'", con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
Label1.Text = reader["FullName"].ToString();
reader.Close();
con.Close();
GetID();
GetCourse();
//con.Open();
//string sql = "SELECT SubjectName,CreditHours FROM Subjects WHERE Course='" + Label5.Text + "'";
//SqlCommand com = new SqlCommand(sql, con);
//SqlDataReader dr;
//dr = com.ExecuteReader();
//CheckBoxList1.DataValueField = "SubjectName";
//CheckBoxList1.DataTextField = "SubjectName";
//CheckBoxList1.DataSource = dr;
//CheckBoxList1.DataBind();
Label6.Visible = true;
Label7.Visible = true;
Label8.Visible = true;
Label9.Visible = true;
Label10.Visible = true;
Label11.Visible = true;
Label12.Visible = true;
Label6.Text = "";
Label7.Text = "";
Label8.Text = "";
Label9.Text = "";
Label10.Text = "";
Label11.Text = "";
Label12.Text = "";
}
public void GetID()
{
Label2.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Student_ID FROM Student WHERE Username= '" + Session["username"].ToString() + "'", con);
Label4.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
public void GetCourse()
{
Label2.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Course FROM Student WHERE Username= '" + Session["username"].ToString() + "'", con);
Label5.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label3.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList1.SelectedValue + "'", con);
Label3.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
Label6.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList3.SelectedValue + "'", con);
Label6.Text = cmd.ExecuteScalar().ToString();
con.Close();
//Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
Label7.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList4.SelectedValue + "'", con);
Label7.Text = cmd.ExecuteScalar().ToString();
con.Close();
//Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
Label8.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList5.SelectedValue + "'", con);
Label8.Text = cmd.ExecuteScalar().ToString();
con.Close();
//Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
{
Label9.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList6.SelectedValue + "'", con);
Label9.Text = cmd.ExecuteScalar().ToString();
con.Close();
//Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
protected void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)
{
Label10.Visible = true;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList7.SelectedValue + "'", con);
Label10.Text = cmd.ExecuteScalar().ToString();
con.Close();
//Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList8.SelectedValue + "'", con);
Label11.Text = cmd.ExecuteScalar().ToString();
con.Close();
// Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
protected void DropDownList9_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CreditHours FROM Subjects WHERE SubjectName= '" + DropDownList9.SelectedValue + "'", con);
Label12.Text = cmd.ExecuteScalar().ToString();
con.Close();
//Label2.Text = Label6.Text + Label7.Text + Label8.Text + Label9.Text + Label10.Text + Label11.Text + Label12.Text;
}
public void add()
{
int val1 = int.Parse(Label6.Text);
int val2 = int.Parse(Label7.Text);
int val3 = int.Parse(Label8.Text);
int val4 = int.Parse(Label9.Text);
int val5 = int.Parse(Label10.Text);
int val6 = int.Parse(Label11.Text);
int val7 = int.Parse(Label12.Text);
//Label2.Text = (val1 + val2 + val3 + val4 + val5 + val6 + val7).ToString();
}
}
}
 
 
I need to add each label value as int and display in label2 as a string.
Example:
 
If Label 6 = 3 and label 7 =2, the label 2 text should show 5. Each time the dropdownlist value is changed the label 2 should update based on the labels  6,7,8,9,10,11,12. If the dropdwon list is empty the label should not take the value for that specific dropdownlist. Example: if label 6 is empty, label 2 should display only the sum of labels 7,8,9,10,11,12 .
 
 
Thank You 
 

Answers (4)