Solved

DbNull and String.Empty throwing an error

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

22 Experts available now in Live!

Get 1:1 Help Now