2015-04-15 45 views
1

我正在使用SQL数据库来存储员工的午餐时间,使用SQL DATEDIFF()函数和总时间。现在如果总午餐时间大于30分钟,则显示的结果应该是红色的。我收到以下错误:SQL DATEDIFF()和asp.net

Input string was not in correct format error

SQL表

name  | lunIn     |  lunOut     | lunTot 
A  | 2014-12-23 13:08:53.323 | 2014-12-23 13:39:42.050  |  31 

ASP.net

int lunTotal = Convert.ToInt16(e.Row.Cells[4].Text); 
if (lunTotal > 30) 
{ 
    e.Row.Cells[4].ForeColor = Color.Red; 
} 
+0

哪条线你会得到这个错误? – sqluser

+0

int lunTotal = Convert.ToInt16(e.Row.Cells [4] .Text); – user1596993

+0

确保使用'Int16.TryParse(e.Row.Cells [4] .Text)填充了'e.Row.Cells [4] .Text'' – sqluser

回答

0

我有null或空数据,因此这是造成错误..这里是解决方案。

if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     int brkDateTotal; 
     bool b = int.TryParse(e.Row.Cells[4].Text, out brkDateTotal); 
     if (brkDateTotal > 15) 
     { 
      e.Row.Cells[4].ForeColor = Color.Red; 
      e.Row.Cells[4].Font.Bold = true; 
     } 
    }