Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Conditional in Loop not working

Reply
Thread Tools

Conditional in Loop not working

 
 
Drew
Guest
Posts: n/a
 
      11-07-2005
I am building an application for keeping track of user permissions here at
work. I have built the interfaces, and am now working on the processing
page for inserting to the database. I am having a problem with a
conditional in my loop not working correctly, I am sure that I have been
overlooking something, but cannot pin it down.


I have pared the code down quite a bit just so that I could resolve my
issues with it. I have a page named addAIMR.asp that has about 34
checkboxes, 2 dropdowns and 2 textboxes. I am trying to something like
this,

If checkbox is checked
AccessOption1 = 1 (which is the access option number)
Else
AccessOption1 = 0
End If

Then I am trying to loop through the code to insert only the ones that <> 0.
This should be rather straight forward, but I am running into issues... I
have inserted many Response.Write's throughout the code to see the
variables, and everything looks kosher, until it gets to the loop, then it
shows that it would insert all the records.

Here is my code for processAIMR.asp,

<%
Dim AIMRDate
Dim AuthName
Dim EmpID
Dim Comments

AIMRDate = Request.Form("Date")
AuthName = Request.Form("AuthName")
EmpID = Request.Form("EmpID")
Comments = Request.Form("Comments")

Dim AccessOption1
Dim AccessOption2
Dim AccessOption3
Dim AccessOption4
Dim AccessOption5

If Request.Form("chkStandard") = "1" Then
Response.Write("1 = Checked<br>")
AccessOption1 = "1"
Else
Response.Write("1 = Not Checked<br>")
AccessOption1 = "0"
End If
If Request.Form("chkEmail") = "1" Then
Response.Write("2 = Checked<br>")
AccessOption2 = "2"
Else
Response.Write("2 = Not Checked<br>")
AccessOption2 = "0"
End If
If Request.Form("chkVPN") = "1" Then
Response.Write("3 = Checked<br>")
AccessOption3 = "3"
Else
Response.Write("3 = Not Checked<br>")
AccessOption3 = "0"
End If
If Request.Form("chkOtherNetwork") = "1" Then
Response.Write("4 = Checked<br>")
AccessOption4 = "4"
Else
Response.Write("4 = Not Checked<br>")
AccessOption4 = "0"
End If
If Request.Form("chkPRAIS") = "1" Then
Response.Write("5 = Checked<br>")
AccessOption5 = "5"
Else
Response.Write("5 = Not Checked<br>")
AccessOption5 = "0"
End If

For i = 1 to 5
If (AccessOption & i) <> "0" Then
Response.Write("AccessOption" & i & " is not empty, therefore will be
inserted<br>")
'Insert 1 record for each option checked
Else
Response.Write("AccessOption" & i & " is empty, therefore will NOT be
inserted<br>")
End If
Next
%>

If I check 2 boxes, chkStandard and chkVPN, then I get the following written
to the page,

1 = Checked
2 = Not Checked
3 = Checked
4 = Not Checked
5 = Not Checked
AccessOption1 is not empty, therefore will be inserted
AccessOption2 is not empty, therefore will be inserted
AccessOption3 is not empty, therefore will be inserted
AccessOption4 is not empty, therefore will be inserted
AccessOption5 is not empty, therefore will be inserted

Can anyone spot where I am going awry?

TIA,
Drew


 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      11-07-2005
Drew wrote on 07 nov 2005 in microsoft.public.inetserver.asp.general:

> AccessOption5 = "0"
> End If
>
> For i = 1 to 5
> If (AccessOption & i) <> "0" Then


You cannot do this:

(AccessOption & i)
This does not make a variable name in ASP-VBS

Use an array:

dim AccessOption()

AccessOption(5) = "0"
i=5
If AccessOption(i) <> "0" Then
....



--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      11-07-2005
"Evertjan." <(E-Mail Removed)> wrote in message
news:Xns9707E7B2EC739eejj99@194.109.133.242...
> Drew wrote on 07 nov 2005 in microsoft.public.inetserver.asp.general:
>
> > AccessOption5 = "0"
> > End If
> >
> > For i = 1 to 5
> > If (AccessOption & i) <> "0" Then

>
> You cannot do this:
>
> (AccessOption & i)
> This does not make a variable name in ASP-VBS


But this does:

If Eval("AccessOption" & i) <> "0" Then


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      11-08-2005
McKirahan wrote:

>> This does not make a variable name in ASP-VBS

>
> But this does:
>
> If Eval("AccessOption" & i) <> "0" Then


Yeah, but Eval is evil
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      11-08-2005
"Bob Barrows [MVP]" <(E-Mail Removed)> wrote in message
news:uBNgEz$(E-Mail Removed)...
> McKirahan wrote:
>
> >> This does not make a variable name in ASP-VBS

> >
> > But this does:
> >
> > If Eval("AccessOption" & i) <> "0" Then

>
> Yeah, but Eval is evil


Why?

I understand that it is deprecated in JavaScript
but this is VBScript and it solves his problem.


 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      11-08-2005
Really overkill here. There is no point using Eval when you can use arrays
instead for both execution efficiency and clarity...

--
Patrice

"McKirahan" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> "Bob Barrows [MVP]" <(E-Mail Removed)> wrote in message
> news:uBNgEz$(E-Mail Removed)...
> > McKirahan wrote:
> >
> > >> This does not make a variable name in ASP-VBS
> > >
> > > But this does:
> > >
> > > If Eval("AccessOption" & i) <> "0" Then

> >
> > Yeah, but Eval is evil

>
> Why?
>
> I understand that it is deprecated in JavaScript
> but this is VBScript and it solves his problem.
>
>



 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      11-08-2005
McKirahan wrote:
> "Bob Barrows [MVP]" <(E-Mail Removed)> wrote in message
> news:uBNgEz$(E-Mail Removed)...
>> McKirahan wrote:
>>
>>>> This does not make a variable name in ASP-VBS
>>>
>>> But this does:
>>>
>>> If Eval("AccessOption" & i) <> "0" Then

>>
>> Yeah, but Eval is evil

>
> Why?


Mainly, because it starts another compiler. Instead of a single compiler
being applied to your code, you now have two compliers being spawned to,
well, compile the code. See:
http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx

Eval has its places, but its use should be considered as an absolute last
resort. In this case, there is a much better solution available (the array),
so eval should not be considered.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      11-08-2005
Bob Barrows [MVP] wrote:
> McKirahan wrote:
>> "Bob Barrows [MVP]" <(E-Mail Removed)> wrote in message
>> news:uBNgEz$(E-Mail Removed)...
>>> McKirahan wrote:
>>>
>>>>> This does not make a variable name in ASP-VBS
>>>>
>>>> But this does:
>>>>
>>>> If Eval("AccessOption" & i) <> "0" Then
>>>
>>> Yeah, but Eval is evil

>>
>> Why?

>
> Mainly, because it starts another compiler. Instead of a single
> compiler being applied to your code, you now have two compliers being
> spawned to, well, compile the code. See:
> http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx
>

Oh! and don't forget to look at Part 2:
http://blogs.msdn.com/ericlippert/ar.../04/53335.aspx

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
Drew
Guest
Posts: n/a
 
      11-09-2005
Thanks for all the replies... I ended up using the array method.

Thanks,
Drew


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Photoshop Javascript Conditional not working bonus Javascript 8 03-26-2010 11:12 PM
Conditional Gridview Edit Column Not Working Kristen ASP .Net Web Controls 0 08-31-2006 10:43 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
IE conditional comments not quite working delerious@no.spam.com HTML 6 12-27-2003 08:21 PM



Advertisments