只要数据库返回结果集,我的代码就可以正常工作。当它什么都不返回时,它会中断并给我以下错误:
System.FormatException: Input string was not in a correct format.
这是我的代码:
DataTable data = GeneralFunctions.GetData( query );
object sumObject;
sumObject = data.Compute( "Sum(Minutes_Spent)", "" );
if ( reportType == 1 )
{
RepeaterPCBillable.DataSource = data;
RepeaterPCBillable.DataBind();
LabelPCBillable.Text = ParseTime( int.Parse( sumObject.ToString() ) ) == null
? ParseTime( int.Parse( sumObject.ToString() ) )
: ""; // ERROR HERE
}
else
{
RepeaterDTSTBillable.DataSource = data;
RepeaterDTSTBillable.DataBind();
LabelDTSTBillable.Text = ParseTime( int.Parse( sumObject.ToString() ) ) == null
? ParseTime( int.Parse( sumObject.ToString() ) )
: "";
}
解析时间:
protected string ParseTime ( int TotalMinutes )
{
int hours = TotalMinutes / 60;
int minutes = TotalMinutes % 60;
if ( hours == 0 )
{
return String.Format( "{0} minutes", minutes );
}
else if ( hours == 1 )
{
return String.Format( "{0} hour and {1} minutes", hours, minutes );
}
else if ( hours > 1 )
{
return String.Format( "{0} hours and {1} minutes", hours, minutes );
}
else
{
return "";
}
}