+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
Web Development Thread, ASP If Statement not working? in Coding and Web Development; hi all, i currently in the process of writing up an electronic system for logging toner usage through out the ...
  1. #1
    Flakes's Avatar
    Join Date
    Nov 2009
    Location
    Newcastle
    Posts
    476
    Thank Post
    38
    Thanked 74 Times in 48 Posts
    Rep Power
    28

    ASP If Statement not working?

    hi all, i currently in the process of writing up an electronic system for logging toner usage through out the school and have hit a snag with some of my code:

    Code:
      
    if fault = fault and black = true Then
        response.write("faulty black") 'test clause
        response.end 'test clause
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
      ElseIf fault = null and black = true Then
        response.write("faulty null, black replaced") 'test clause
        response.end ' test clause
        rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1
    End if
    the above if statement is the part of my code that is not working, for some strange reason the code within the if statement is not executed even when the values for fault and black correspond to the if statement criteria.

    i tested these values further up to see if i could identify the problem but i cant see anything wrong with the code.

    Code:
    Dim fault
    Dim black
    fault = Request.Form("fault")
    black = Request.Form("black")
    
    response.write ("val fault: " & fault & "<br />black: " & black)
    response.end 'test clause
    the above code verified that black = true and fault = fault so i have no idea why the if statement is being ignored. Any ideas?

  2. #2
    tommccann's Avatar
    Join Date
    Jun 2009
    Posts
    252
    Thank Post
    46
    Thanked 39 Times in 31 Posts
    Rep Power
    0
    i know that many different coding languages now that i get constantly muggled up but shouldnt it be ==

    please ignore my ramblings if i am way off

  3. #3

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    Firstly, it looks like you're comparing fault to itself. Is this what you're wanting to do? Secondly, try wrapping them in brackets:

    Code:
    if (fault = fault) and (black = true) Then

  4. #4

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by tommccann View Post
    i know that many different coding languages now that i get constantly muggled up but shouldnt it be ==

    please ignore my ramblings if i am way off
    It looks like Visual Basic, so the equality operator is =.

  5. #5
    Flakes's Avatar
    Join Date
    Nov 2009
    Location
    Newcastle
    Posts
    476
    Thank Post
    38
    Thanked 74 Times in 48 Posts
    Rep Power
    28
    actually webman i think your right, asp is probably seeing both as the variable rather than the variable and contents...

    tommccann, i know what you mean but in asp it is just one = sign.

  6. #6
    u8dmtm's Avatar
    Join Date
    Feb 2006
    Posts
    231
    Thank Post
    7
    Thanked 13 Times in 12 Posts
    Rep Power
    20
    Does this work?:

    Code:
    if fault = "fault" and black = true Then
        response.write("faulty black") 'test clause
        response.end 'test clause
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
    Else
        If fault = null and black = true Then
            response.write("faulty null, black replaced") 'test clause
            response.end ' test clause
            rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1
        End if
    End if

  7. #7
    Flakes's Avatar
    Join Date
    Nov 2009
    Location
    Newcastle
    Posts
    476
    Thank Post
    38
    Thanked 74 Times in 48 Posts
    Rep Power
    28
    sorry lads got called off to refill some toner cartridges (ironic really) >.<

    changed fault to equal true instead of fault(also changed on the form) and using this code:

    Code:
      
    if fault = true and black = true Then
        response.write("faulty black")
        response.end
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
      ElseIf fault = null and black = true Then
        response.write("faulty null, black replaced")
        response.end
        rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1
      End if
    still no different.

    ill try it with the brackets, see if that makes a difference (didnt know you could use them in asp?)

    ARGH! *bangs head... still not working, tried with this:

    Code:
      
    if (fault = true) and (black = true) Then
        response.write("faulty black")
        response.end
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
       Else
       if (fault = null) and (black = true) Then
         response.write("faulty null, black replaced")
        response.end
        rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1 
      End if
    and this

    Code:
      if (fault = true) and (black = true) Then
        response.write("faulty black")
        response.end
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
       ElseIf (fault = null) and (black = true) Then
         response.write("faulty null, black replaced")
        response.end
        rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1 
      End if
    and i just realised i made a slight error with the way this works, when a faulty cartridge is replaced it only will record one instance, ill need to turn that into a int instead(thats easily solved)

    heres the whole code just incase ive missed something obvious:

    Code:
    <%
    'process_room.asp
    
    
    'Dimension variables
    Dim adoCon          'Holds the Database Connection Object
    Dim rsAddRoom       'Holds the recordset for the new record to be processed
    Dim strSQL          'Holds the SQL query to query the database
    
    'get form values
    Dim roomid
    Dim printid
    Dim tonerid
    Dim fault
    Dim black
    Dim cyan
    Dim megenta
    Dim yellow
    Dim year
    roomid = Request.Form("roomid")
    printid = Request.Form("printid")
    tonerid = Request.Form("tonerid")
    fault = Request.Form("fault")
    black = Request.Form("black")
    cyan = Request.Form("cyan")
    megenta = Request.Form("megenta")
    yellow = Request.Form("yellow")
    year = Request.Form("year")
    
     	'Create an ADO connection object
    Set adoCon = Server.CreateObject("ADODB.Connection")
    
    'Set an active connection to the Connection object using a DSN-less connection
    adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("toner.mdb")
    
    'Create an ADO recordset object
    Set rsAddRoom = Server.CreateObject("ADODB.Recordset")
    
     	'Initialise the strSQL variable with an SQL statement to query the database
    strSQL = "SELECT * FROM used WHERE roomid =" & roomid
    
    'response.write ("val fault: " & fault & "<br />black: " & black)
    'response.end
    
    	'Set the cursor type we are using so we can navigate through the recordset
    rsAddRoom.CursorType = 2
     	
    	'Set the lock type so that the record is locked by ADO when it is updated
    rsAddRoom.LockType = 3
    
    'Open the recordset with the SQL query
    rsAddRoom.Open strSQL, adoCon
    
    if rsAddroom.EOF OR rsAddRoom.BOF Then
    
      rsAddroom.Close
      
      'response.write("end of File")
      'response.end
      
      'Initialise the strSQL variable with an SQL statement to query the database
      strSQL = "SELECT * FROM used;"
    
      'Set the cursor type we are using so we can navigate through the recordset
      rsAddRoom.CursorType = 2
        
      'Set the lock type so that the record is locked by ADO when it is updated
      rsAddRoom.LockType = 3
    
      'Open the recordset with the SQL query
      rsAddRoom.Open strSQL, adoCon
    
      'Tell the recordset we are adding a new record to it
      rsAddRoom.AddNew
    
      'Add a new record to the recordset
      rsAddRoom.Fields("roomid") = roomid
      rsAddRoom.Fields("tonerid") = tonerid
      rsAddRoom.Fields("printid") = printid
      rsAddroom.Fields("year") = year
      
      if (fault = true) and (black = true) Then
        response.write("faulty black")
        response.end
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
      Else
      if (fault = null) and (black = true) Then
        response.write("faulty null, black replaced")
        response.end
        rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1
      End if
      
      if fault = true and cyan = true Then
        rsAddRoom.fields("faultycyan") = rsAddRoom("faultycyan") + 1
      ElseIf fault = null and cyan = true Then
        rsAddRoom.fields("usedcyan") = rsAddRoom("usedcyan") + 1
      End if
      
      if fault = true and megenta = true Then
        rsAddRoom.fields("faultmegenta") = rsAddRoom("faultmegenta") + 1
      ElseIf fault = null and megenta = true Then
        rsAddRoom.fields("usedmegenta") = rsAddRoom("usedmegenta") + 1
      End if
      
      if fault = true and yellow = true Then
        rsAddRoom.fields("faultyellow") = rsAddRoom("faultyellow") + 1
      ElseIf fault = null and yellow = true Then
        rsAddRoom.fields("usedyellow") = rsAddRoom("usedyellow") + 1
      End if
    
        'Write the updated recordset to the database
      rsAddRoom.Update
    
        'Reset server objects
      rsAddRoom.Close
      Set rsAddRoom = Nothing
      Set adoCon = Nothing
    
    else
    
      'response.write("else statement")
      'response.end
    
        'Tell the recordset we are adding a new record to it
      rsAddRoom.AddNew
    
        'Add a new record to the recordset
      rsAddRoom.Fields("roomid") = roomid
      rsAddRoom.Fields("tonerid") = tonerid
      rsAddRoom.Fields("printid") = printid
      rsAddRoom.Fields("year") = year
        
      if (fault = true) and (black = true) Then
        response.write("faulty black")
        response.end
        rsAddRoom.fields("faultyblack") = rsAddRoom("faultyblack") + 1
       Else
       if (fault = null) and (black = true) Then
         response.write("faulty null, black replaced")
        response.end
        rsAddRoom.fields("usedblack") = rsAddRoom("usedblack") + 1 
      End if
        
      if fault = true and cyan = true Then
        rsAddRoom.fields("faultycyan") = rsAddRoom("faultycyan") + 1
      ElseIf fault = null and cyan = true Then
        rsAddRoom.fields("usedcyan") = rsAddRoom("usedcyan") + 1
      End if
      
      if fault = true and megenta = true Then
        rsAddRoom.fields("faultmegenta") = rsAddRoom("faultmegenta") + 1
      ElseIf fault = null and megenta = true Then
        rsAddRoom.fields("usedmegenta") = rsAddRoom("usedmegenta") + 1
      End if
      
      if fault = true and yellow = true Then
        rsAddRoom.fields("faultyellow") = rsAddRoom("faultyellow") + 1
      ElseIf fault = null and yellow = true Then
        rsAddRoom.fields("usedyellow") = rsAddRoom("usedyellow") + 1
      End if
    
        'Write the updated recordset to the database
      rsAddRoom.Update
    
        'Reset server objects
      rsAddRoom.Close
      Set rsAddRoom = Nothing
      Set adoCon = Nothing
    
    End if
    
    'Redirect to the default.asp page
    Response.Redirect "Default.asp"
    %>
    Last edited by Flakes; 26th March 2010 at 12:17 PM.

  8. #8
    u8dmtm's Avatar
    Join Date
    Feb 2006
    Posts
    231
    Thank Post
    7
    Thanked 13 Times in 12 Posts
    Rep Power
    20
    Is this still a problem? If so post the error message.

    Also, have you not considered moving over to ASP.Net?

  9. #9
    Flakes's Avatar
    Join Date
    Nov 2009
    Location
    Newcastle
    Posts
    476
    Thank Post
    38
    Thanked 74 Times in 48 Posts
    Rep Power
    28
    Quote Originally Posted by u8dmtm View Post
    Is this still a problem? If so post the error message.

    Also, have you not considered moving over to ASP.Net?
    yep still an issue, unfortunately there is no error message, the if statement above just does not seem to do anything, however the entry in the table "used" is created. the Response.write and response.end should tell me what part of the If statement is being executed(although i know response.end stops the execution there)

  10. #10

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    Explicitly set fault and black to TRUE just above the IF statement. I'm thinking you might need to compare strings (and not a boolean type such as true/false), depending on what the form values are. E.g.

    Code:
    if fault = "true" and black = "true" then.....

  11. #11
    u8dmtm's Avatar
    Join Date
    Feb 2006
    Posts
    231
    Thank Post
    7
    Thanked 13 Times in 12 Posts
    Rep Power
    20
    I'm thinking you might need to compare strings
    One reason why ASP.Net is so much better than ASP as you have properly defined variables there.

  12. #12

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,417
    Thank Post
    644
    Thanked 965 Times in 665 Posts
    Blog Entries
    2
    Rep Power
    328
    Or even better, PHP or Python

  13. #13
    pwds's Avatar
    Join Date
    Dec 2008
    Location
    Derby
    Posts
    279
    Thank Post
    73
    Thanked 48 Times in 38 Posts
    Rep Power
    21
    Do you have HP printers? HP Web jet admin will do all of this for you and is supported.

  14. #14

    Join Date
    May 2009
    Location
    Sheffield
    Posts
    274
    Thank Post
    27
    Thanked 39 Times in 29 Posts
    Rep Power
    22
    Have you tried just outputting the values of your variables in a response page to see what's coming out?

    I'm with webman, I think that fault will either be set to the string "True", or to a numeric value equivalent to True (normally -1, 0 or 1 depending on the language).

  15. #15
    Flakes's Avatar
    Join Date
    Nov 2009
    Location
    Newcastle
    Posts
    476
    Thank Post
    38
    Thanked 74 Times in 48 Posts
    Rep Power
    28
    Quote Originally Posted by MattMitchell View Post
    Have you tried just outputting the values of your variables in a response page to see what's coming out?
    yea i tried that, and it all checked out. Im going to try some of the suggestions here on Monday when i get back to work and see if they work. Never bring work home with me.



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Multiple Values In Case KixStart Statement
    By DaveP in forum How do you do....it?
    Replies: 2
    Last Post: 14th January 2009, 05:39 PM
  2. CV personal statement section
    By ITWombat in forum Educational IT Jobs
    Replies: 16
    Last Post: 17th June 2008, 07:35 AM
  3. Best Mission Statement Words
    By blackcat in forum General Chat
    Replies: 10
    Last Post: 22nd December 2007, 11:30 AM
  4. Vision Statement
    By paulpmp4 in forum Virtual Learning Platforms
    Replies: 5
    Last Post: 29th October 2007, 11:50 AM
  5. Excel IF statement maybe VB
    By danIT in forum General Chat
    Replies: 1
    Last Post: 5th January 2007, 01:12 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •