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

时间:2015-11-25 05:15:07

标签: c# asp.net webforms

我有以下c#代码,应将fee_paid_int输出为'20',将total_student_int输出为'30'我在调试代码时遇到以下错误{ {1}}

int fee_paid_int = Convert.ToInt32(fee_paid_string.Trim());

C#

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

//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,我们将非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

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();

答案 1 :(得分:0)

替换代码中的以下行。

//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 + "%";