Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DbNull and String.Empty throwing an error

Posted on 2008-10-13
4
Medium Priority
?
1,712 Views
Last Modified: 2012-06-22
Hi all,

I have some data from an old website that is not in the best condition. Somehow over the years null values in the DB have been replaced with empty strings, so when querying someones address and wanting it not to show empty lines I cannot check for Nulls'. I came up with this work around in the T-SQL:

address1 = case len(address1)
                        when 0 then null
                        else address1
                  end

Which successfully replaces the lines with Nulls, but when running the page I'm getting an error:

<%#iif(DbNull.Value.Equals(Eval("address2")), String.Empty, CStr(Eval("address2")) & "<br />")%>

"Conversion from type 'DBNull' to type 'String' is not valid. But surely the iif should be returning a String.Empty or the DB value, not trying to convert it?
0
Comment
Question by:G0ggy
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22702837
This this may be returning NULL as len(NULL) is NULL not 0 unless I am mistaken.

I would do this:
(this will catch NULL and empty string together -- alternatively, you can just use the IsNull construct to return empty string and test for empty string in code versus NULL but that is up to you)
address1 = case IsNull(address1,'')
                        when '' then null
                        else address1
                  end

Open in new window

0
 
LVL 3

Expert Comment

by:3abqari
ID: 22702865
Try this:

Eval("address2") Is Nothing

instead of:

DbNull.Value.Equals(Eval("address2"))
0
 
LVL 11

Accepted Solution

by:
G0ggy earned 0 total points
ID: 22702938
It was the CStr on the negative iif value, but thanks for your help anyway.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22704690
Didn't think that looked right.  Should be Eval("address2").ToString(), but glad you found the cause of the problem.  
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question