2015-04-15 48 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; 
     } 
    }