Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide null values in vbscript

Status
Not open for further replies.

puremm

Technical User
Oct 30, 2006
23
GB
How do I hide a null value in a recordset field? I've got a page that pulls information from a table, but certian fields have no values "NULL". When I create a detail page I want to hide that value if it has nothing or null data.

Thanks in advanced...

p.s. search not working, and already spent a hour looking...
 
What do you mean by hiding the value? If the value is NULL then it does not have a value. If you concatenate this into a string then it acts like an empty string, so nothing comes out...

I'm confused...? Do you have an example of what is happening now that you would like to correct?

 
did you mean if the whole column has null or empty values, you want to hide the column from the display...

if this is the case...then I have posted the code in one of my previous posts for the exactly similar situation...

please search for it...i will post the link to the thread if i find it...

-DNG
 
Apologies if I've not explain very well; what I'm trying to do is hide a telephone fields that are not empty, for example in a directory some clients only have one telephone while other have two or three; but this on the page leaves a nb_return where the other fields should be.

See code below;

<h3>Contact</h3>
<cite><%=(details.Fields.Item("Contact").Value)%></cite>
<h3>Telephone</h3>
<cite><%=(details.Fields.Item("Telephone1").Value)%>
<% If Not isnull (details.Fields.Item("Telephone2").Value) Then %>
<br /><%=(details.Fields.Item("Telephone2").Value)%>
<% End If %>
<% If Not isnull (details.Fields.Item("Telephone3").Value) Then %>
<br /><%=(details.Fields.Item("Telephone3").Value)%>
<% End If %>
</cite>

Thanks for the replies
 
There are two cases that you will want to hide the additional fields on: nulls and empty values. The quick an easy way to do this is to concatenate the field with an empty string. This forces VBScript to change NULLs into an empty string (if it is null) and doesn't affect the string at all if it is anything else. Then you can simply check that the length of the string is positive:
Code:
<% If Len("" & details.Fields.Item("Telephone2").Value) > 0 Then %>
<br /><%=(details.Fields.Item("Telephone2").Value)%>
<% End If %>


Escaping back and forth between ASP and HTML is generally considered to be bad practice. For one thing it causes a little extra load on the server processing the page, kind of like constantly shifting gears in your car. It also is harder to read. My general rule of thumb is that I want to be able to look at a piece of code in 6 months and be able to figure it out easily, if I can't it is either too complicated or needs commenting.

In this case I would rewrite the section above as:
Code:
<% 
If Len("" & details.Fields.Item("Telephone2").Value) > 0 Then 
   Response.Write "<br />" & (details.Fields.Item("Telephone2").Value)
End If

This will additionally cut down on the extra spaces or carriage returns that your send to the client inside tags that render them. For instance:
Code:
<br /><%=(details.Fields.Item("Telephone2").Value)%>
<% End If %>
<% If Not isnull (details.Fields.Item("Telephone3").Value) Then %>
Each of these lines jumps back out to HTML at the end, has a carriage return, then goes back into HTML. That means you have added 3 carriage returns for no reason. While this may seem minor, it's just one more reason not to constantly jump in and out of ASP blocks.

-T

 
Thanks for the replies, you're advice helped me alot in getting the correct solution for the project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top