472,096 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Asp eliminating blank lines in database listing

I'm a newbie to ASP and databases. I have created a simple contact list database and an asp page that shows the contact listing. So far so good.

Here's what I have:
Expand|Select|Wrap|Line Numbers
  1. <%=rs.fields("CompanyName")%> <BR>
  2. <%=rs.fields("FirstName")%>&nbsp;<%=rs.fields("LastName")%> <BR>
  3. <%=rs.fields("Title")%> <BR>
  4. <%=rs.fields("Address")%> <BR>
  5. <%=rs.fields("Address2")%>  <BR>
  6. <%=rs.fields("Address3")%>  <BR>
  7. <%=rs.fields("Country")%>  <BR>
  8. <%=rs.fields("City")%> <%=rs.fields("State")%> <%=rs.fields("Zip")%> <BR>
  9. <%=rs.fields("State")%>  <BR>
  10. <%=rs.fields("Zip")%> <BR>
  11. Tel:&nbsp;<%=rs.fields("WorkPhone")%> &nbsp;or&nbsp;<%=rs.fields("WorkPhone2")%> <BR>
  12. Mobile:&nbsp;<%=rs.fields("MobilePhone")%> <BR>
  13. Fax:&nbsp;<%=rs.fields("FaxNumber")%> Email:&nbsp;<a href="mailto:<%=rs.fields("EmailName")%>"><%=rs.fields("EmailName")%></a><BR>
  14.  
The problem I have is that I want to skip the field altogether if there is no data in that field. This is the kind of result that I am trying to clean up:

Coburn Graphic Films, Incorporating.


1650 Corporate Road West



Lakewood NJ 08701
NJ
08701
Tel: 404-656-3855 or
Mobile: 404-656-3856
Fax: 404-656-7916 Email: gach@opb.state.ga.us

I guess I need and if, then, else statement to skip the blanks, but I don't know how to do it. Can anyone help?

Thanks!

-S
Aug 30 '07 #1
8 1707
ilearneditonline
130 Expert 100+
You will need to check if they exist first....

Expand|Select|Wrap|Line Numbers
  1.  <% if LEN(rs.fields("Zip")) > 0 then 
  2. Response.Write(rs.fields("Zip")
  3. end if%>
  4.  
Or you could write a function to handle this.
Expand|Select|Wrap|Line Numbers
  1.  function valueExists(input){ 
  2. if LEN(input) > 0 then
  3. Response.Write(input)
  4. end if
  5. }
  6.  
  7. ' call the function
  8. valueExists(rs.fields("Zip")) 
  9.  
Aug 30 '07 #2
Thanks for your reply. I believe your If statement will check to see if the field is greater than 0 and if so it displays the field. What I'm trying to do is a little bit more than that. If the field is greater than 0 then I would like to add some formatting code as well such as a label, the database field and then a break. So I'm looking to say this:

If the address field is greater than zero then type:
"Address: - <display field contents> <BR>"

Can you expand on your answer? Thanks!
Aug 30 '07 #3
ilearneditonline
130 Expert 100+
Thanks for your reply. I believe your If statement will check to see if the field is greater than 0 and if so it displays the field. What I'm trying to do is a little bit more than that. If the field is greater than 0 then I would like to add some formatting code as well such as a label, the database field and then a break. So I'm looking to say this:

If the address field is greater than zero then type:
"Address: - <display field contents> <BR>"

Can you expand on your answer? Thanks!
Expand|Select|Wrap|Line Numbers
  1.  function valueExists(input, formatbegin, formatend){ 
  2.   if LEN(input) > 0 then
  3.     Response.Write(formatbegin & input & formatend)
  4. end if
  5. }
  6.  
  7. ' call the function
  8. valueExists(rs.fields("Zip"), "Zip Code: ","<br />") 
  9.  
Aug 30 '07 #4
Sadly I am too inexperienced to put your suggested into use. I'm flailing over here trying to get this to work. Is there any assistance you can offer? It seems a simple business, but I'm obviously missing something. I'd be happy to pay you to write this page for me.

Thanks!

-Tracy
Aug 30 '07 #5
markrawlingson
346 Expert 100+
Tracy,

Here's your example... Which will simply display everything, including fields which have no value.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%=rs.fields("CompanyName")%> <BR>
  3. <%=rs.fields("FirstName")%>&nbsp;<%=rs.fields("LastName")%> <BR>
  4. <%=rs.fields("Title")%> <BR>
  5. <%=rs.fields("Address")%> <BR>
  6. <%=rs.fields("Address2")%> <BR>
  7. <%=rs.fields("Address3")%> <BR>
  8. <%=rs.fields("Country")%> <BR>
  9. <%=rs.fields("City")%> <%=rs.fields("State")%> <%=rs.fields("Zip")%> <BR>
  10. <%=rs.fields("State")%> <BR>
  11. <%=rs.fields("Zip")%> <BR>
  12. Tel:&nbsp;<%=rs.fields("WorkPhone")%> &nbsp;or&nbsp;<%=rs.fields("WorkPhone2")%>
  13.  
  14.  
What he's suggesting that you do is have your code check to see if a field is empty before displaying it - such as the below...

Expand|Select|Wrap|Line Numbers
  1. <%
  2. If LEN(rs.fields("CompanyName")) > 0 Then 
  3.      Response.Write "Company Name: " & rs.fields("CompanyName")
  4. End If
  5.  
In other words... If there is even 1 character in "CompanyName" - it will be displayed, if not, it will not be displayed. You can format it however you like.
Aug 30 '07 #6
jhardman
3,406 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. <%
  2. If LEN(rs.fields("CompanyName")) > 0 Then 
  3.      Response.Write "Company Name: " & rs.fields("CompanyName")
  4. End If
  5.  
In other words... If there is even 1 character in "CompanyName" - it will be displayed, if not, it will not be displayed. You can format it however you like.
I usually do something more like:
Expand|Select|Wrap|Line Numbers
  1. for each x in rs.fields
  2.    if trim(rs(x)) <> "" then response.write x & ": " & rs(x) & "<br>" & vbNewLine
  3. next
this will print out every field (if it isn't empty). If a user inputs a space in an input, the script might interpret this as important and save it to the db, in which case you do have a value: " ". My method will eliminate those as well. Of course, it is a good idea to trim every input right before you save it...

Jared
Aug 30 '07 #7
markrawlingson
346 Expert 100+
That's a really good way of doing it actually.. that will totally elimate your problem Tracy.

I'm going to adapt this method in my own work, Jared. Never thought of doing it that way before.

Mark
Aug 30 '07 #8
Genius! Even I understood that. Thank you so much!!
Aug 31 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Melissa | last post: by
5 posts views Thread by Justin Fancy | last post: by
3 posts views Thread by ShaeMills via AccessMonster.com | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.