0

I want to display every thing in database to asp.net. But some of my value are null and some not. When I load to the page that null it will cause error. May I know how can I fix this problem? My mean is to display everything from database doesn't care it is NULL or having content.

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

The t.FileUpload in my database some of row are null and some contain contain. And the error is

system.invalidcastexception unable to cast object of type 'system.dbnull' to type 'system.string'
4
  • 1
    Code please, and exception details. Commented Nov 21, 2013 at 15:47
  • Please post some of the code to understand better. Commented Nov 21, 2013 at 15:47
  • Are you binding detail with some data control of asp.net? Commented Nov 21, 2013 at 15:47
  • You may find this useful: dev.mysql.com/doc/refman/5.0/en/working-with-null.html Commented Nov 21, 2013 at 15:48

1 Answer 1

1

Use Standard SQL coalesce function.

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

it will return the first not null value.

Coalesce in mySQL

or just simply:

Coalesce (a.Picture, '') as Picture

This will prevent invalidcastexception

Sign up to request clarification or add additional context in comments.

4 Comments

but then now having another problem System.IndexOutOfRangeException. Could not find specific column......
use alias: Coalesce (a.Picture, '') as Picture
No probs, enjoy it ;)
Accept the answers in your questions, if you are satisfied with them please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.