Solved

DbNull and String.Empty throwing an error

Posted on 2008-10-13
4
1,709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

623 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