0

I have the following c# code which should output fee_paid_int as '20' and total_student_int as '30' how ever I am receiving the following error when I debug the code on int fee_paid_int = Convert.ToInt32(fee_paid_string.Trim());

System.FormatException: Input string was not in a correct format.

C#

//Total Student's - Not counting Graduated, Withdrawn or Temporarily Withdrawn
DataView t_stu = (DataView)total_students_count.Select(DataSourceSelectArguments.Empty);
    foreach (DataRowView t_stu_sql in t_stu)
    {
        total_students_count_label.Text = "Total Current Students: <b>" + t_stu_sql["total_students"].ToString() + "</b><p></p>";
    }

// Total Fee's Paid to Date
DataView f_paid = (DataView)fees_paid_count.Select(DataSourceSelectArguments.Empty);
    foreach (DataRowView fee_paid_sql in f_paid)
    {
        fees_paid_count_label.Text = "Total Fee's Paid to date: <b>" + fee_paid_sql["fee_paid"].ToString() + "</b><p></p>";
    }

int fee_paid_int = Convert.ToInt32(total_students_count_label.Text.Trim());
int total_student_int = Convert.ToInt32(fees_paid_count_label.Text.Trim());
int fee_paid_percent = (int)Math.Round((double)(100 * fee_paid_int) / total_student_int);
fee_paid_percent_label.Text = "Total Percent of Student's who have fully paid their Tution Fee to date " + fee_paid_percent + "%";

int fee_paid_percent should equal 66.66, any help would be greatly appreciated.

2 Answers 2

1
total_students_count_label.Text = "Total Current Students: <b>" +
 t_stu_sql["total_students"].ToString() + "</b><p></p>";

The contents of this control cannot be converted to int because this contains non-numeric and non-integer contents..

you may need to revise this to change total_students_count_label.Text to contain only numbers - in your case... it should be

total_students_count_label.Text = t_stu_sql["total_students"].ToString();
Sign up to request clarification or add additional context in comments.

Comments

0

Replace below line in your code.

//Total Student's - Not counting Graduated, Withdrawn or Temporarily Withdrawn
DataView t_stu =   (DataView)total_students_count.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView t_stu_sql in t_stu)
{
    total_students_count_label.Text = t_stu_sql["total_students"].ToString();
}

  // Total Fee's Paid to Date
DataView f_paid = (DataView)fees_paid_count.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView fee_paid_sql in f_paid)
{
    fees_paid_count_label.Text = fee_paid_sql["fee_paid"].ToString(); 
}

int fee_paid_int = Convert.ToInt32(total_students_count_label.Text.Trim());
int total_student_int = Convert.ToInt32(fees_paid_count_label.Text.Trim());
int fee_paid_percent = (int)Math.Round((double)(100 * fee_paid_int) /    total_student_int);
fee_paid_percent_label.Text = "Total Percent of Student's who have fully paid their Tution Fee to date " + fee_paid_percent + "%";

1 Comment

The variables such as 't_stu_sql' would not exist in this context as they are located within the foreach statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.