无法将字符串转换为 int。错误消息:输入字符串的格式不正确

Not able to convert string to int. Error msg: Input string was not in a correct format

我有以下 C# 代码,它应该输出 fee_paid_int 作为 '20'total_student_int 作为 '30' 如何在调试代码时收到以下错误在 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 应等于 66.66,如有任何帮助,我们将不胜感激。

替换代码中的下行。

//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 + "%";
total_students_count_label.Text = "Total Current Students: <b>" +
 t_stu_sql["total_students"].ToString() + "</b><p></p>";

无法将此控件的内容转换为 int,因为它包含非数字和非整数内容..

您可能需要修改它以将 total_students_count_label.Text 更改为仅包含数字 - 在您的情况下......应该是

total_students_count_label.Text = t_stu_sql["total_students"].ToString();