Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Session variables

    i am trying to implement a session variable that displays the username. I have a global.asa file but i don't know how this will fit in:


    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <script language="vbscript" runat="server">
    Sub Application_OnStart
    Application("Username")
    End SubSub Session_OnStart
    Application.Lock
    Application("Username")=Application("Username")
    Application.UnLock
    End SubSub Session_OnEnd
    Application.Lock
    Application("Username")=Application("Username")-1
    Application.UnLock
    End Sub</script>

    </body>
    </html>

    I would be grateful if you guys can help me ammend this script. thanks

  • #2
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Please use tags.
    The global.asa file is not suppose to have HTML code inside of it.

    Look here, you'll find your answer at W3Schools.com:
    http://www.w3schools.com/asp/asp_globalasa.asp

    Also, you can't use the global.asa to write the username to a page. All you have to do is go into your display page and put in
    Code:
    <%
    Response.Write Session("name")
    %>

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks i tried it but it just displays blank. I know in coldfusion you have to define session variables; is it the same in asp and if so can you please show me how to do it? thanks

  • #4
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Ok.

    When you open up your database because the user has logged in, all you have to do is store his username into a session variable.
    Code:
    <%
    Set oConn = Server.CreateObject("ADODB.Connection") 
    StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb") & ";" 
    oConn.open StrConn
    
    SQL = "SELECT Username FROM tblTable WHERE something = something"
    Set oRS = oConn.Execute(SQL)
    
    Session("Username") = oRS("Username")
    %>
    And then when you want to display that variable, all you have to do is:
    Code:
    <%
    Response.Write Session("Username")
    %>
    Make sence?

  • #5
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, this is the code i have tried including the session inside the script:

    <%@Language=VBScript%>

    <%
    username = REPLACE(request.form("username"), "'", "''")
    password= REPLACE(request.form("password"), "'", "''")

    Set oConn = Server.CreateObject("ADODB.Connection")
    StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("netteh.mdb") & ";"
    oConn.open StrConn

    SQL = "SELECT * FROM Students WHERE Username = Username"
    Set oRS = oConn.Execute(SQL)

    Session("Username") = oRS("Username")

    If rs.EOF = false then
    'it found a record, so the login info is correct
    Session("name") = rs("Firstname")
    Else
    'it did NOT find a record so let them know it was an invalid entry
    Response.Redirect("frontpage.asp")
    End if

    rs.Close
    conn.Close
    set rs=nothing
    set conn=nothing
    %>

    but it comes up with errors. Please can you help fix the code? thanks
    Also, i would like to display the results of a table in access. Just like coldfusion. How can i do this?
    Last edited by sagat; 08-26-2004 at 11:48 PM.

  • #6
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by sagat
    but it comes up with errors. Please can you help fix the code? thanks
    Also, i would like to display the results of a table in access. Just like coldfusion. How can i do this?
    Please, for god's sake, use tags when you post code. It's so much easier to read, and indents show up in code tags.

    Also, please tell us what error you get. If you get and error message, or the code is not doing what you want it to do.

    Everything in bold is what I edited or added.
    Code:
    <%@Language=VBScript%>
    
    <%
    StrUsername = REPLACE(request.form("username"), "'", "''")
    StrPassword = REPLACE(request.form("password"), "'", "''")
    
    Set oConn = Server.CreateObject("ADODB.Connection") 
    StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("netteh.mdb") & ";" 
    oConn.open StrConn
    
    SQL = "SELECT * FROM Students WHERE Username = " & StrUsername
    Set oRS = oConn.Execute(SQL)
    
    Session("Username") = oRS("Username")
    
    If rs.EOF = False then
      'it found a record, so the login info is correct
      Session("Firstname") = oRS("Firstname")
    Else
      'it did NOT find a record so let them know it was an invalid entry
      Response.Redirect("frontpage.asp")
    End if
    
    Response.Write Session("Username")
    Response.Write "<br>"
    Response.Write Session("Firstname")
    
    oRS.Close
    oConn.Close
    Set oRS = Nothing
    Set oConn = Nothing
    
    %>

  • #7
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks i tried it but it came up with the following error:

    Error Type:
    Microsoft JET Database Engine (0x80040E14)
    Syntax error (missing operator) in query expression 'Username ='.
    /validate.asp, line 12


    Line 12 is: SQL = "SELECT * FROM Students WHERE Username = " & StrUsername

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your db-column "Username" will be of a stringtype, so the value needs to be enclosed in quotes.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i put the qoutes and it came up with errors:
    SQL = "SELECT * FROM Students WHERE Username = & StrUsername"
    Set oRS = oConn.Execute(SQL)

    Error Type:
    Microsoft JET Database Engine (0x80040E14)
    Syntax error (missing operator) in query expression 'Username = & StrUsername'.
    /TMPlq3xv32rru.asp, line 12

    and i tried:
    SQL = "SELECT * FROM Students WHERE Username" = "& StrUsername"
    Set oRS = oConn.Execute(SQL)

    Error Type:
    Microsoft JET Database Engine (0x80040E14)
    Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
    /TMPlsdiw32rus.asp, line 12
    Last edited by sagat; 08-27-2004 at 12:32 AM.

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try

    SQL = "SELECT * FROM Students WHERE Username = '" & StrUsername & "'"
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #11
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still has errors: Error Type:
    Error Type:
    Microsoft VBScript compilation (0x800A0400)
    Expected statement
    /TMPlykmp32s2t.asp, line 11
    "SELECT * FROM Students WHERE Username = '" & StrUsername & "'"

    SQL = "SELECT * FROM Students WHERE Username = '" & StrUsername & "'"
    Last edited by sagat; 08-27-2004 at 12:36 AM.

  • #12
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yaeh! Still errors. But we moved on 3 lines ! Think positive !
    The query should be valid now (which doesn't mean it'll return records).

    Anyway, this looks wrong

    "If rs.EOF = False then"

    i don't see you open na recordset named "rs". You probably copy pasted this code from somewhere. Your recordset is called oRS so the chack should probably be

    "If oRS.EOF = False then"

    <edit>Hah. You edited your post to get an error o the query

    echo out the composed sql and then copy paste it to a querywindow in your db to see if it's valid. Or maybe post the code you now have because i'm quite sure you'll still have other errors</edit>
    Last edited by raf; 08-27-2004 at 12:43 AM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #13
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is what i have:

    <%@Language=VBScript%>

    <%

    ' variables
    dim oConn
    dim rs
    dim StrUsername
    dim StrPassword
    dim StrConn
    dim oRS
    dim sqlStr

    StrUsername = REPLACE(request.form("Username"), "'", "''")
    StrPassword = REPLACE(request.form("Password"), "'", "''")

    Set oConn = Server.CreateObject("ADODB.Connection")
    StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("netteh.mdb") & ";"
    oConn.open StrConn

    sqlStr = "Select * From Students where StrUsername = '" _
    & Request.Form("username") & "' and password = '" & Request.Form("password") & "'"
    Set oRS = oConn.Execute(SQL)

    Session("Username") = oRS("Username")

    If rs.EOF = False then
    'it found a record, so the login info is correct
    Session("Firstname") = oRS("Firstname")
    Else
    'it did NOT find a record so let them know it was an invalid entry
    Response.Redirect("frontpage.asp")
    End if


    Response.Write Session("Username")
    Response.Write "<br>"
    Response.Write Session("Firstname")


    oRS.Close
    oConn.Close
    Set oRS = Nothing
    Set oConn = Nothing

    %>

    Error Type:
    Microsoft JET Database Engine (0x80040E0C)
    Command text was not set for the command object.
    /TMP2jbr233dly.asp, line 24


    line 24: Set oRS = oConn.Execute(SQL)

  • #14
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm. I don't think you realy know what you are doing.

    If you copy some code, but change the variablenames, then you of course need to change them throughout your code. The error you get on

    Set oRS = oConn.Execute(SQL)
    is caused by the "SQL". This was the variablename in your original code, that contained the sql-query as a stringvalue. In your latest version of that code, you rename that variable to "sqlStr".

    So either rename the sqlStr back to SQL or change the
    Set oRS = oConn.Execute(SQL)
    into
    Set oRS = oConn.Execute(sqlStr)

    then you have this buggy line
    Session("Username") = oRS("Username")
    which should be moved a fex lines down to the if-clause (you can only call the querystringvalue if there is a record returned, so you would get an error if no records are returned)

    The next error that you'll get will point to this line
    If rs.EOF = False then
    which should be
    If oRS.EOF = False then

    your selectquery should also be rewritten to

    sqlStr = "Select TOP 1 Username, Firstname From Students where StrUsername = '" _
    & Request.Form("username") & "' and password = '" & Request.Form("password") & "' ORDER BY Username ASC"

    or else you need to check if the returned recordcount is higher then 1 before processing it
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #15
    New Coder
    Join Date
    Jan 2004
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys this is the code i have now:

    <%@Language=VBScript%>

    <%
    StrUsername = REPLACE(request.form("username"), "'", "''")
    StrPassword = REPLACE(request.form("password"), "'", "''")

    Set oConn = Server.CreateObject("ADODB.Connection")
    StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("netteh.mdb") & ";"
    oConn.open StrConn

    sqlStr = "Select TOP 1 Username, Firstname From Students where StrUsername = '" _
    & Request.Form("username") & "' and password = '" & Request.Form("password") & "' ORDER BY Username ASC"
    Set oRS = oConn.Execute(SQL)


    Session("Username") = oRS("Username")

    If oRS.EOF = False then
    'it found a record, so the login info is correct
    Session("Username") = oRS("Username")

    Else
    'it did NOT find a record so let them know it was an invalid entry
    Response.Redirect("frontpage.asp")
    End if

    Response.Write Session("Username")
    Response.Write "<br>"
    Response.Write Session("Firstname")

    oRS.Close
    oConn.Close
    Set oRS = Nothing
    Set oConn = Nothing

    %>

    Error Type:
    Microsoft JET Database Engine (0x80040E0C)
    Command text was not set for the command object.
    /TMP5fch13401e.asp, line 13


    Set oRS = oConn.Execute(SQL)


    This is the current snag; if you guys could rewrite or correct it i would be grateful. thanks


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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