Print Page | Close Window

if - else - elseif - confused!

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=7026
Printed Date: 31 March 2026 at 11:58am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: if - else - elseif - confused!
Posted By: leeolive
Subject: if - else - elseif - confused!
Date Posted: 05 November 2003 at 12:27pm

Hi

Can someone please help?

I keep getting an error saying 'expected end line 1508' which is at the bottom of my page.

My code is so incredible long otherwise I would post it here. Do you have any ideas?

Thanks!

if str="a" or str="b" or str="c" then
  printForm()
response.end

else
  set rs = cn.execute("select * from myTable where ni = '" & ni & "' or un = '" & un & "' or email = '" & email & "'")
  if rs.eof then
    cn.execute("INSERT myTable VALUES('" & ni & "','" & email & "'")


  else
    if rs("ni") = ni then
      print table saying you are already registered

    elseif rs("un") = un then
      print table saying username in use
    elseif rs("email") = email then
      print table saying email in use

  else
      print table saying incorrect

  end if
end if

</BODY>
</HTML>




Replies:
Posted By: PeterE
Date Posted: 05 November 2003 at 1:44pm
Originally posted by leeolive leeolive wrote:

Hi

Can someone please help?

I keep getting an error saying 'expected end line 1508' which is at the bottom of my page.

My code is so incredible long otherwise I would post it here. Do you have any ideas?

Thanks!

if str="a" or str="b" or str="c" then
  printForm()
response.end

else
  set rs = cn.execute("select * from myTable where ni = '" & ni & "' or un = '" & un & "' or email = '" & email & "'")
  if rs.eof then
    cn.execute("INSERT myTable VALUES('" & ni & "','" & email & "'")


  else
    if rs("ni") = ni then
      print table saying you are already registered

    elseif rs("un") = un then
      print table saying username in use
    elseif rs("email") = email then
      print table saying email in use

  else
      print table saying incorrect

  end if
end if

</BODY>
</HTML>

Unless I am very mistaken this code is never going to run, first you've got the nesting of the if ... then ... elseif ... else wrong.

Second dunno what you want achieve with the commands inside the the criteria selections. dunno a better word. lol

Well if ya want to do it this way then try something like this

<%
If Str = "a" Or Str = "b" Or Str = "c" Then
    'printForm()
    response.End
Else
    Set rs = cn.execute("select * from myTable where ni = '" & ni & "' or un = '" & un & "' or email = '" & email & "'")
    If rs.EOF Then
        cn.execute ("INSERT myTable VALUES('" & ni & "','" & email & "'")
    ElseIf rs("ni") = ni Then
        'Print Table saying you are already registered
    ElseIf rs("un") = un Then
        'print table saying username in use
    ElseIf rs("email") = email Then
        'print table saying email in use
    Else
        'Print Table saying incorrect
    End If
End If
%>

commented out the strange print commands cause they would never run. You could try a response.write("blabla")

Further more creating an object for your connection and recordset would also help

If Str = "a" Or Str = "b" Or Str = "c" Then
    printForm()
    response.End
Else
    Set rs = cn.execute("select * from myTable where ni = '" & ni & "' or un = '" & un & "' or email = '" & email & "'")
    If rs.EOF Then
        cn.execute ("INSERT myTable VALUES('" & ni & "','" & email & "'")
    Else
        If rs("ni") = ni Then
             Print Table; saying; you; are; already; registered
        ElseIf rs("un") = un Then
             print table saying username in use
        ElseIf rs("email") = email Then
             print table saying email in use
        Else
             Print Table; saying; incorrect
        End If
    End If
End If

Or did you mean this.

Last but not least if your code is so long try splitting it up in logical units and include the files as you go along.

Be sure to take care of any variables if you do.

Hope this helped

 

 



Posted By: leeolive
Date Posted: 05 November 2003 at 2:44pm

Thanks for getting back. In english (with a bit of code) I need to do this

'server side validation - check to see if form fields have been completed or not

if strusername = "" or strpassword = "" then

<table>I say within my html table - please complete fields!</table>

response.end ' this stop record being added if field is empty and user has to go back to form

else

'check to see if anyone is in db according to ni number. Also check to see if anyone is using the username or password. Can't have 2 people with same un and pw combination!

set up cn and run command to retrieve values where rscheckuser("ninumber") =  strninumber or rscheckuser("username") = strusername or rscheckuser("password") = strpassword or rscheckuser("Email") = stremail

if no matching value is found then

run command and submit data to db

if a matching value is retrieved then

if rscheckuser("ninumber") = strninumber then

<table>you are already registered</table>

if rscheckuser("username") = strusername then

<table>please enter a different username</table>

if rscheck("userpassword") = strpassword then

<table>please enter a different password</table>

if rscheck("email") = stremail then

<table>email already in use</table>

Surely people have come up against this? It does seem complicated but I need to do these checks and just not sure how and when to do them if I don't do them this way. I think the problem kicks in after the dat is submitted to database(if no one with same ni number found in db). Or, can I do a whole series of these instead of if then else.

if... then...end if

if...then...end if

 

Thanks for your help! Appreciate it.

Lee

 

 



Posted By: MorningZ
Date Posted: 05 November 2003 at 8:56pm

welll, looking at your first post of code...  you have three opening "if" statements and only two "end if" statements....

so low and behold, the server is telling you:
"expected end line 1508"

amazing



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: leeolive
Date Posted: 06 November 2003 at 2:05am

That was the first thing I tried. I added one <%end if%> and it still kept asking for 'expected end' so added another and another. Still keeps asking. Can you take a look at my code and see if you can spot anything? Where it says<table></table> is where I print my whole html table with a message

'do server side validation in javascript turned off
if strContractedInIndicator = "" or strFirstName = "" then
%>
<table>
this says : please enter required text
</table>

<%response.end
else
strContractedInIndicator = "0"

'set command and establish connection
Set cndb = server.CreateObject("adodb.connection")
Set cmdcheckuser = server.CreateObject("adodb.command")
Set cmdaddtodatabase = server.CreateObject("adodb.command")
Set cmdretrievefromdb = server.CreateObject("adodb.command")
Set rsrecordsretrieved = server.CreateObject("adodb.recordset")
Set rscheckuser = server.CreateObject("adodb.recordset")

'set connection to local DNS or change to web host DNS
cndb.CommandTimeout = 15
cndb.CursorLocation = adUseServer
cndb.ConnectionString = "DSN=cjrcls" 'this is for local
'cndb.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mydatasourcehere.mdb"
on error resume next
cndb.Mode = 3 '3 = adModeReadWrite
cndb.open

if err <> 0 then
response.write "Error connecting to the database"
response.end
end if

With cmdcheckuser
.CommandTimeout = 15
set .ActiveConnection = cndb
.CommandType = adCmdtext
.CommandText = "SELECT * FROM ApplicantDetails WHERE NINumber = '" & strNINumber & "' OR UserName = '" & strUserName & "' OR UserPassword = '" & strUserPassword & "' OR EmailContact = '" & strEmailContact & "';"
End with

rscheckuser.Open cmdcheckuser,,adOpenStatic,adLockOptimistic

on error resume next

if err.number <> 0 then
response.write "error connecting to database<br>" &_
"Error: " & err.Description
response.end
end if

'if can't find a record with matching NINumber then user is not in db so submit their details
if rscheckuser.EOF = true or rscheckuser.BOF = true then

'pass values as parameters
With cmdaddtodatabase
.CommandTimeout = 15
set .ActiveConnection = cndb
.CommandType = adCmdText
.CommandText = "INSERT INTO ApplicantDetails (ContractedInIndicator .....My Note: This all works perfectly..passes parameters etc)


on error resume next
.Execute

if err.number <> 0 then
Response.write err.description
Response.End
end if

'if was successfully submitted then redirect to process page with NINumber
if err.number = 0 then
id = strNINumber
Response.redirect "process.asp?id=" & Encrypt(strNINumber) 'added
Response.End
end if

End with

elseif rscheckuser("NINumber") = strNINumber then

%>
<!-- if persons NINumber has been found in db then tell them they are already registered! -->

<table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left"> This says: you are already registered!
</table>

<%elseif rscheckuser("UserName") = strUserName then%>

<table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left">This says please enter a different username </table>

<%elseif rscheckuser("UserPassword") = strUserPassword then
%>

<table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left">This says please enter a different password </table>

<%elseif rscheckuser("EmailContact") = strEmailContact then
%>

<table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left">This says that the email is already in use </table>

<%end if%>
<%end if%>
<%end if%>


</body>
</html>

 



Posted By: PeterE
Date Posted: 06 November 2003 at 3:45am

Maybe this will help, i adjusted your tabs so that you can see where the nesting end should be, dunno if the code will work but you added one end if to much anyway. here it is.

<!-- Added this Line --> <%


'do server side validation in javascript turned off
if strContractedInIndicator = "" or strFirstName = "" then
%>
 <table>
  this says : please enter required text
 </table>
<%
 response.end
else
 strContractedInIndicator = "0"

 'set command and establish connection
 Set cndb = server.CreateObject("adodb.connection")
 Set cmdcheckuser = server.CreateObject("adodb.command")
 Set cmdaddtodatabase = server.CreateObject("adodb.command")
 Set cmdretrievefromdb = server.CreateObject("adodb.command")
 Set rsrecordsretrieved = server.CreateObject("adodb.recordset")
 Set rscheckuser = server.CreateObject("adodb.recordset")

 'set connection to local DNS or change to web host DNS
 cndb.CommandTimeout = 15
 cndb.CursorLocation = adUseServer
 cndb.ConnectionString = "DSN=cjrcls" 'this is for local
 'cndb.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mydatasourceher e.mdb"
 on error resume next
 cndb.Mode = 3 '3 = adModeReadWrite
 cndb.open

 if err <> 0 then
  response.write "Error connecting to the database"
  response.end
 end if

 With cmdcheckuser
  .CommandTimeout = 15
  set .ActiveConnection = cndb
  .CommandType = adCmdtext
  .CommandText = "SELECT * FROM ApplicantDetails WHERE NINumber = '" & strNINumber & "' OR UserName = '" & strUserName & "' OR UserPassword = '" & strUserPassword & "' OR EmailContact = '" & strEmailContact & "';"
 End with

 rscheckuser.Open cmdcheckuser,,adOpenStatic,adLockOptimistic
 on error resume next
 
 if err.number <> 0 then
  response.write "error connecting to database<br>" &_
   "Error: " & err.Description
  response.end
 end if

 'if can't find a record with matching NINumber then user is not in db so submit their details
 if rscheckuser.EOF = true or rscheckuser.BOF = true then
  'pass values as parameters
  With cmdaddtodatabase
   .CommandTimeout = 15
   set .ActiveConnection = cndb
   .CommandType = adCmdText
   .CommandText = "INSERT INTO ApplicantDetails (ContractedInIndicator .....My Note: This all works perfectly..passes parameters etc)

   on error resume next
   .Execute

   if err.number <> 0 then
    Response.write err.description
    Response.End
   end if

   'if was successfully submitted then redirect to process page with NINumber
   if err.number = 0 then
    id = strNINumber
    Response.redirect "process.asp?id=" & Encrypt(strNINumber) 'added
    Response.End
   end if
  End with

 elseif rscheckuser("NINumber") = strNINumber then
%>
  <!-- if persons NINumber has been found in db then tell them they are already registered! -->
  <table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left" >
    This says: you are already registered!
  </table>
<%
 elseif rscheckuser("UserName") = strUserName then
%>
  <table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left">
   This says please enter a different username
  </table>
<%
 elseif rscheckuser("UserPassword") = strUserPassword then
%>
  <table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left">
   This says please enter a different password
  </table>
<%
 elseif rscheckuser("EmailContact") = strEmailContact then
%>
  <table class="bylinefont" width="100%" bgcolor="white" border="0" cellpadding="0" cellspacing="0" align="left">
   This says that the email is already in use
  </table>
<%
 end if
end if
%>




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net