baxtalo
asked on
Classic ASP - If Else Conditional Statement
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.
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"%>
<%=rs("First_Name")%>
<%end if%>
Can anyone please help?
Thank you.
That could be written like this, by the way:
<%
If Pref_First_Name = "" then
Response.Write(rs("First_N ame"))
Else
Response.Write (rs(" Pref_First_Name"))
End If
%>
<%
If Pref_First_Name = "" then
Response.Write(rs("First_N
Else
Response.Write (rs(" Pref_First_Name"))
End If
%>
ASKER
It gives me this error message:
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
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.
ASKER
I used this but it won't display the first name:
<%
If Pref_First_Name = "" then
Response.Write(rs("First_N ame"))
Else
Response.Write (rs("Pref_First_Name"))
End If
%>
<%
If Pref_First_Name = "" then
Response.Write(rs("First_N
Else
Response.Write (rs("Pref_First_Name"))
End If
%>
ASKER
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
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.
<%
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.
Does your query include a field called First_Name? Are you sure it isn't empty?
ASKER
Now the First_Name displays ::::
The Pref_First_Name is displayed like this :Jimmy:
The Pref_First_Name is displayed like this :Jimmy:
ASKER
The fist name column is not empty, everbody has a first name as it is a mandatory field.
They're backwards then. You must be calling the first name "Pref_First_Name" and the other "First Name".
ASKER
This is how preferred is displayed
::Jimmy::
::Jimmy::
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.
ASKER
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
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
Ah, try this:
<%
If ISNULL(rs("Pref_First_Name ")) then
Response.Write(rs("First_N ame"))
Else
Response.Write (rs("Pref_First_Name"))
End If
%>
<%
If ISNULL(rs("Pref_First_Name
Response.Write(rs("First_N
Else
Response.Write (rs("Pref_First_Name"))
End If
%>
ASKER
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 />
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 />
Okay, let's try reversing the logic:
<%
If ISNULL(rs("First_Name")) then
Response.Write (rs("Pref_First_Name"))
Else
Response.Write(rs("First_N ame"))
End If
%>
<%
If ISNULL(rs("First_Name")) then
Response.Write (rs("Pref_First_Name"))
Else
Response.Write(rs("First_N
End If
%>
ASKER
This one displays the first name of everybody, but the preferred first name is not displayed for those who have preferred first names.
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_N ame"))
Else
Response.Write (rs("Pref_First_Name"))
End If
%>
<%
If ISNULL(rs("Pref_First_Name
Response.Write(rs("First_N
Else
Response.Write (rs("Pref_First_Name"))
End If
%>
ASKER
It gives me this error:
Microsoft VBScript compilation error '800a03ee'
Expected ')'
/ATO/Profile/Profile_View_ allNOCC.as p, line 206
If ISNULL(rs("Pref_First_Name ")) OR TRIM(rs("Pref_First_Name") = "" Then
Microsoft VBScript compilation error '800a03ee'
Expected ')'
/ATO/Profile/Profile_View_
If ISNULL(rs("Pref_First_Name
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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.
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")
Glad I could help.
<% If Pref_First_Name = "" then %>
<% Response.Write(rs("First_N
<% Else %>
<% Response.Write (rs(" Pref_First_Name")) %>
<% End If %>