2015-11-25 46 views
0

我有應該輸出fee_paid_int'20'total_student_int'30'怎麼過我收到以下錯誤下面的C#代碼時,我調試的代碼int fee_paid_int = Convert.ToInt32(fee_paid_string.Trim());無法將字符串轉換爲int。錯誤信息:輸入字符串的不正確的格式

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,任何幫助,將不勝感激。

回答

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

啊,當然,這是一個非常愚蠢的錯誤。感謝您解決這個問題。 – mcclosa

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

如「t_stu_sql」在這種情況下就不會存在,因爲他們所在的foreach語句中的變量。 – mcclosa