2013-11-21 67 views
0

我想顯示數據庫中的每一件事情到asp.net。但我的一些價值是無效的,有些不是。當我加載到空的頁面時會導致錯誤。我可以知道我該如何解決這個問題?我的意思是顯示一切從數據庫不關心它是NULL或有內容。從asp.net數據庫中顯示爲空

SELECT m.PersonID, m.Picture,m.PersonName, t.title, t.Fileupload, t.contentBody,  t.dateInserted FROM person m, thread t WHERE m.PersonID = t.PersonID AND t.threadID = @TID 

t.FileUpload在我的數據庫中有一些行是空的,有些是包含的。並且錯誤是

system.invalidcastexception unable to cast object of type 'system.dbnull' to type 'system.string' 
+1

代碼,請和異常細節。 – speti43

+0

請張貼一些代碼以更好地理解。 –

+0

你是否綁定了一些asp.net數據控件的細節? –

回答

1

使用標準SQL合併函數。

SELECT a.PersonID, 
Coalesce (a.Picture, othercolumn, anothercolumn, 'if all are null') as Picture 
from tableA a 

它將返回第一個非空值。

Coalesce in mySQL

或只是簡單:

Coalesce (a.Picture, '') as Picture 

這將防止InvalidCastException的

+0

但現在有另一個問題System.IndexOutOfRangeException。找不到具體欄目...... – user2769165

+0

使用別名:coalesce(a.Picture,'')如圖 – speti43

+0

非常感謝。其工作:D – user2769165