Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A newbie's conditional statement

Status
Not open for further replies.

trufla

Programmer
Aug 9, 2004
31
GB
Hello,

could someone help? I have a simple if statement for error handling that basically states:

If a page exists display the page, if it does not display an error. I cannot get this to work. The logic escapes me for some reason and it is doing my head in!


Code:
Dim page2, page3, page4, page5, page6, page7, page8, page9, page10, page11, page12, page13, page14, page15, page16, page17
		page2 = Request.QueryString("PageID") <> 2 
		page3 = Request.QueryString("PageID") <> 3 
		page4 = Request.QueryString("PageID") <> 4 
		page5 = Request.QueryString("PageID") <> 5 
		page6 = Request.QueryString("PageID") <> 6 
		page7 = Request.QueryString("PageID") <> 7 
		page8 = Request.QueryString("PageID") <> 8 
		page9 = Request.QueryString("PageID") <> 9 
		page10 = Request.QueryString("PageID") <> 10 
		page11 = Request.QueryString("PageID") <> 11 
		page12 = Request.QueryString("PageID") <> 12 
		page13 = Request.QueryString("PageID") <> 13 
		page14 = Request.QueryString("PageID") <> 14 
		page15 = Request.QueryString("PageID") <> 15 
		page16 = Request.QueryString("PageID") <> 16 
		page17 = Request.QueryString("PageID") <> 17 
		
		Dim URL
		URL = "pagename.asp?" & request.ServerVariables("QUERY_STRING")
		
If page2 or page3 or page4 or page5 or page6 or page7 or page8 or page9 or page10 or page11 or page12 or page13 or page14 or page15 or page16 or page17 Then
			
			response.redirect("page_error.asp")
		            Else
				
				response.redirect(URL)

End If


This code works if I remove the "Or" conditions and will redirect 1 page. But if I try to run the condition with or, the page_error.asp page displays regardless.


Any help appreciated!
 
hmm...i dont understand what you are trying to do here

what does this line doing:

page2 = Request.QueryString("PageID") <> 2

-DNG
 
Maybe I have done this incorrently but I am trying to say:

PageID is not equal to 2. Put that fact into a variable called page 2. Then in the If statement say:

If page2 (i.e. Request.QueryString("PageID") <> 2) Then

//Do this....


It was just to save doing If Request.QueryString("PageID")<> 2 or Request.QueryString("PageID")<> 3 ...... Then
// Do this......
 
try this:
Code:
Dim qstring, URL
qstring = request.ServerVariables("QUERY_STRING")
URL = "pagename.asp?" & qstring

if qstring<>2 or qstring<>3....so on then      response.redirect("page_error.asp")
                    Else
                response.redirect(URL)
End If

-DNG
 
Dim page
page = CInt(Request.QueryString("PageID"))

If page < 2 OR page > 17 Then
Response.redirect("page_error.asp")
Else
Response.Redirect(URL)
End


Something like that maybe?
 
First of all:
The syntax of each of the page assignment lines will work. The boolean result of the conditional will be assigned to the variable correctly.

Second:
While the syntax works, the logic is all wrong. If you expand the statements out you will find that the final or statement should always result in a true value, resulting in a redirect to the error page.

Logical Error Example:
"Page is not 2 OR Page is not 3"
This statement will always be true because the only time it will false is if Page were able to simultaneously be 2 and 3 to force both statements to False. Meaning that no matter what value "page" is, this statement will always be true.


Quick Fix:
1) Change all of the lines to page2 = (Request.QueryString("PageID") = 2) and hope that VBScript understands you want a conditional check, not an assignment.
2) Replace the whole thing with ASPFun's example. Except you may want to make sure it is a number before tossing it into a cInt()


I apologize if this was long-winded, but I feel that knowing why something is breaking is often more useful than knowing another method to get the desired result.

-T


 
Aaah, thanks guys. I will try your fixes and see what works.

Tarwn, thanks for your explanation. I figured I had gone wrong in the logic somewhere but didn't think of the condition always resulting in true.

Uuuuummmm, well I shall try and use your insight to get a true or false result.


Cheers!



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top