We help IT Professionals succeed at work.

Classic ASP - If Else Conditional Statement

baxtalo
baxtalo asked
on
I have problems retrieving the right data from my database fields.
I have two colums: Pref_First_Name and First_Name
If someone has a preferred first name I want it displayed in the browser, but if they don't have a preferred first name I want their first name displayed. I was trying something like this but it's not working:
<%if Pref_First_Name = "" then%><%=rs("First_Name"%><%else%>
<%=rs("First_Name")%>
<%end if%>
Can anyone please help?
Thank you.
Comment
Watch Question

Paul MacDonaldDirector, Information Systems

Commented:
How about this:
<% If Pref_First_Name = "" then %>
<% Response.Write(rs("First_Name")) %>
<% Else %>
<% Response.Write (rs(" Pref_First_Name")) %>
<% End If %>
Paul MacDonaldDirector, Information Systems

Commented:
That could be written like this, by the way:
<%
If Pref_First_Name = "" then
  Response.Write(rs("First_Name"))
Else
  Response.Write (rs(" Pref_First_Name"))  
End If
%>  

Author

Commented:
It gives me this error message:
ADODB.Recordset error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.
Paul MacDonaldDirector, Information Systems

Commented:
It looks like I left a space in rs("Pref_First_Name").  If you copied and pasted, look for any spaces/mispellings, fix them, and then see if it works.

Author

Commented:
I used this but it won't display the first name:


<%
If Pref_First_Name = "" then
  Response.Write(rs("First_Name"))
Else
  Response.Write (rs("Pref_First_Name"))
End If
%>

Author

Commented:
It displays the preferred first name if they have one, but no first name is displayed if they don't have a preferred first name
Paul MacDonaldDirector, Information Systems

Commented:
Try this and see if anything displays:
<%
If Pref_First_Name = "" then
  Response.Write(":" & rs("First_Name") & ":")
Else
  Response.Write ("::" & rs("Pref_First_Name") & "::")
End If
%>
If you get two colons, the Response.Write is working, but First_Name is blank.  If you get four colons, same thing for Pref_First_Name.
Paul MacDonaldDirector, Information Systems

Commented:
Does your query include a field called First_Name?  Are you sure it isn't empty?

Author

Commented:
Now the First_Name displays ::::
The Pref_First_Name is displayed like this :Jimmy:

Author

Commented:
The fist name column is not empty, everbody has a first name as it is a mandatory field.
Paul MacDonaldDirector, Information Systems

Commented:
They're backwards then.  You must be calling the first name "Pref_First_Name" and the other "First Name".

Author

Commented:
This is how preferred is displayed
::Jimmy::



Paul MacDonaldDirector, Information Systems

Commented:
Okay, are you sure your select statement calls the column "First_Name"?  I'm sure the code is solid, and the Response.Write is working, but what you're telling it to write is empty.

Author

Commented:
I'm using this on the page:

First Name:<%=rs("First_Name") %><br />
Preferred Name: <% = rs("Pref_First_Name") %>

When someone doesn't have a preferred first name the it's not displayed, but the first name is always displayed, regardless if they have a pref_first_name or not
Paul MacDonaldDirector, Information Systems

Commented:
Ah, try this:
<%
If ISNULL(rs("Pref_First_Name")) then
 Response.Write(rs("First_Name"))
Else
 Response.Write (rs("Pref_First_Name"))
End If
%>

Author

Commented:
This again works fine for people who have pref_first_name
but for people who don't have pref_first_name it doesn't show anything.

In spite of the fact that I can retrieve their first names on the same page like this:

First Name:<%=rs("First_Name") %><br />
Paul MacDonaldDirector, Information Systems

Commented:
Okay, let's try reversing the logic:

<%
If ISNULL(rs("First_Name")) then
Response.Write (rs("Pref_First_Name"))
Else
Response.Write(rs("First_Name"))
End If
%>  

Author

Commented:
This one displays the first name of everybody, but the preferred first name is not displayed for those who have preferred first names.
Paul MacDonaldDirector, Information Systems

Commented:
Heh.  Okay so something is going on with Pref_First_Name.  It isn't null and it isn't empty. Try this:
<%
If ISNULL(rs("Pref_First_Name")) OR TRIM(rs("Pref_First_Name") = "" Then
  Response.Write(rs("First_Name"))
Else
  Response.Write (rs("Pref_First_Name"))
End If
%>

Author

Commented:
It gives me this error:


Microsoft VBScript compilation error '800a03ee'

Expected ')'

/ATO/Profile/Profile_View_allNOCC.asp, line 206

If ISNULL(rs("Pref_First_Name")) OR TRIM(rs("Pref_First_Name") = "" Then

Director, Information Systems
Commented:
Right-o.  Forgot a parens:
<%
If ISNULL(rs("Pref_First_Name")) OR TRIM(rs("Pref_First_Name")) = "" Then
 Response.Write(rs("First_Name"))
Else
 Response.Write (rs("Pref_First_Name"))
End If
%>

Author

Commented:
PERFECT!!!!!!!!

Thank you for your patience, this drove me crazy.

I've been playing with it since yesterday. Why do you think it didn't display it with the methods we tried earlier?

I'm really grateful
Paul MacDonaldDirector, Information Systems

Commented:
First off, we were evaluating Pref_First_Name = "" when we should have been evaluating rs("Pref_First_Name") = "".  
Secondly, rs("Pref_First_Name") must have a space in it because it never comes back as null or empty.  Doing the TRIM(rs("Pref_First_Name")) removes any spaces before or after so it can evaluate to empty.  You'll probably never need the ISNULL check, but leave it in there just in case.
Glad I could help.