Solved

If Then Else Syntax Question

Posted on 2006-07-11
4
877 Views
Last Modified: 2012-05-05
I can't seem to get the syntax right.  I am populating from a DB recordset.  What I'd like to achieve is if either the individual recordset column is blank or the recordset itself is "broken" or "missing" then in its place would be: "No Record" rather than the page throwing an error.  Here's what I've been playing with:

<%
Dim rsHeat
Dim rsHeat_cmd
Dim rsHeat_numRows

Set rsHeat_cmd = Server.CreateObject ("ADODB.Command")
rsHeat_cmd.ActiveConnection = MM_heat_STRING
rsHeat_cmd.CommandText = "SELECT * FROM display"
rsHeat_cmd.Prepared = true

Set rsHeat = rsHeat_cmd.Execute
rsHeat_numRows = 0
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>

<% If (rsHeat.Fields.Item("Heat").Value) = "" Then
     If (rsHeat_total = 0) Then %>
         No Record
         <% Else  %>
<%=(rsHeat.Fields.Item("Heat").Value)%>
  <% End If  
End If%>

</body>
</html>
<%
rsHeat.Close()
Set rsHeat = Nothing
%>
0
Comment
Question by:TCCIRM
  • 2
4 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 50 total points
ID: 17083909
TCCIRM,

It looks like you are using vbscript and ASP.  The parenthesis are not needed around the condition in the If statement.  Also, why are you doing the second If statement?  Do you want it to use OR?  The code below may work better for you.  I will assume that you want No Record to print if the value is blank or the total is 0.

<% If rsHeat.Fields.Item("Heat").Value = "" OR rsHeat_total = 0 Then
%>
         No Record
         <% Else  %>
<%= rsHeat.Fields.Item("Heat").Value%>
  <% End If  
End If%>

Let me know if you have any questions.

b0lsc0tt
0
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 50 total points
ID: 17083911
You've probably got a null in the value - null doesn't equal ""

You check for a null like this:

If IsNull(rsHeat.Fields.Item("Heat").Value) = True Then
0
 

Author Comment

by:TCCIRM
ID: 17084107
Using a combination of both:

<% If IsNull(rsHeat.Fields.Item("Heat").Value) = True OR rsHeat_total = 0 Then %>
         No Record
         <% Else  %>
<%= rsHeat.Fields.Item("Heat").Value%>
<%  
End If%>

However........ when the DB record not "null" and the recordset is not "0" then it should populate from whatever is in the "Heat" field.  It does not.  It writes "No Record" to the page.  
0
 

Author Comment

by:TCCIRM
ID: 17084271
I think my "recordset total" statement is wrong.  If I break the recordset I get an error.  Let me play some more.  Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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