Solved

DbNull and String.Empty throwing an error

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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