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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Connecting to a Database Using ASP

Status
Not open for further replies.

TheStandard

Programmer
Mar 9, 2004
12
US
When I try connecting to a database with a login program, I receive the following error:

Error (424) in Microsoft VBScript runtime error Object required

Here's the code I used:

Code:
<%@LANGUAGE = VBScript %>

<% 
   Option Explicit

   Dim connection, rs

   On Error Resume Next
   Session( "errorString" ) = ""

   Set connection = Server.CreateObject( "ADODB.Connection" ) 
   connection.provider = "Microsoft.Jet.OLEDB.4.0"
   connection.Open "C:\Inetpub\[URL unfurl="true"]wwwroot\currentdatabase.mdb"[/URL]

   Call errorHandlerLog()

   Set rs = Server.CreatObject( "ADODB.Recordset" )
   rs.activeConnection=connection 
   Set Session( "rs" ) = rs

   dim username
   dim password

   username = request.QueryString("username")
   password = request.queryString("password")

   query = "select * from LoginData where username ='" & username & "'" & " AND password ='" & password & "'"
   
   rs.source = query
   rs.open

   Call errorHandlerLog()

   Sub errorHandlerLog()
      If Err.Number <> 0 Then
         Dim errorString
		
         errorString = Session( "errorString" )
	 errorString = errorString & "<p class = " & Chr( 34 ) & "error" & Chr ( 34 ) & ">Error (" & Err.Number & ") in " & Err.source & "<br />" & Err. Description & "</p><br />"
	 Session( "errorString" ) = errorString
	  
	 if rs.eof and rs.bof then
            response.write("Incorrect Username or Password")
           else 
            response.Redirect("../menu.html")
           end if
           connection.close

      End If
   End Sub
%>






Code:
<% @Language = VBScript %>

<% option explicit 

   Session( "query" ) = "SELECT username FROM loginData"
   Call Server.Execute( "database.asp" )
%>

<html>
<head>
<title>Login Page</title>

<style>
   hr     { size: 1 ; color:blue }
   p      { font-family:arial ; font-size: 14pt ; color:blue }
   table  { text-align:center }
   td     { font-size:12pt }
   .font  { font-family: arial }
   .error { color: red }
</style>

</head>
<body
	
    <POSITION: absolute; TOP: 0px div align="left" a href="#"></a><img src="image004.png" name=image" width="378" height="104" border="0" align="left"><br>
    <POSITION: absolute; TOP: 0px div align="right" a href="#"><img src=image004a.png width="595" height="35"></p>
</div>
<br>
<br>
<br>
<br>
<hr>
<br>

<%
   If Session( "errorString" ) = "" Then
   If session( "loginFailure" ) = True Then 
%>

<p 
class = "error">Login attempt failed, please try again
</p>

<%	
   End If
   'begin the form	
%>

<p>
Please select your name and enter your password to login:
</p>
<br />

<form action = "submitlogin.asp" method = "post">
   <table border = "0">
      <tr>
         <td>
            <select name = "username">
            
            <option value = "noSelection">
	    Select your name
            </option>
<%
   If Request.Cookies( "username" ) <> "" Then 
      Call BuildReturning()
    Else
      Call BuildNewUser()
   End If
%>
            </select>
        </td>
      </tr>

       <tr>
          <td>
             Password:
          </td>
          <td>
             <input type = "password" name = "password" />
          </td>
       </tr>

      <tr>
					     
         <td align = "left">
            <input type = "submit" value = "Log Me In" />
         </td>
     </tr>
   </table>
</form>

<%
    Else
      Call Response.Write( Session( "errorString" ))
    End If
%>

</body>
</html>

<%
   Sub BuildReturning()
   Dim found, loginData
   Set loginData = Session( "loginData" )

   found = false

   while Not loginData.EOF
   '	create this record's dropdown entry
%>		
<%	   		
   '	If we did not write selected for any option before
	
   If (Not found ) Then
		
   '  if the current record's username is equal to the username cookie, then it is
   '  the username of the returning user, and thus we need to write selected for this
   '  option; in this case we also need to signal that we have written selected for an
   '  option by setting setting found to True.

      If Request.Cookies( "username" ) = loginData( "username" ) Then
         Call Response.Write( "selected = " & Chr(34 ) & "selected"  & Chr( 34 ) )
			found  = True
      End If
   End If
%>		
   <option Value ="<% =loginData( "username" ) %>">
   <% = loginData( "username" ) %></option>
<%		
   Call loginData.MoveNext()
   Wend
   End Sub
	  
   ' builds the option items for usernames without writing selected for any username
	  
   Sub BuildNewUser()
   Dim loginData
		
   Set loginData = Session( "loginData" )
			
   ' pull user names from the record set to populate the dropdown list
   
   while Not loginData.EOF
			
   'create this record's dropdown entry
%>		
   <option value ="<% =loginData( "username" ) %>">
   <% =loginData( "username" ) %>
   </option>
<%		
   Call loginData.MoveNext()
   Wend
   End Sub
%>
 
To get help, you might want to take time to identify what line or lines of code are producing your error.

For further guidelines, you might want to read faq-2244, in particluar the following:

<14. If you are asking a question related to code, please post the shortest example of code that will reproduce the problem you're experiencing. Include relevant declarations and controls that are affected.

No offense intended, I hope you don't take any.

HTH

Bob
 
Oh, and it is a very bad idea in ASP to store any object in a session variable, ie: [tt]Set Session( "rs" ) = rs[/tt]

If you want to use a session variable as a flag to see if teh user is logged then it is far better to use a text value like the username.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top