Solved

DbNull and String.Empty throwing an error

Posted on 2008-10-13
4
1,702 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 59

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 59

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now