首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

总是报错“缺少对象rs”,该怎么解决

2012-02-28 
总是报错“缺少对象rs”我做的是一个身份验证窗口数据库连接文件是conn.asp%SetconnServer.Createobject(

总是报错“缺少对象rs”
我做的是一个身份验证窗口
数据库连接文件是conn.asp
<%
Set   conn   =   Server.Createobject( "ADODB.Connection ")
connstr   =   "Provider=SQLOLEDB;Data   Source=(local);database=test;user   id=sa;password= ' '; "
conn.open   connstr
%>
登陆窗口文件是login.asp

<html>

<head>
<title> 登陆窗口 </title>
<!--#include   file= "conn.asp "-->
</head>

<body>
<form   name= "form1 "   method= "post "   action= "login.asp?action=checkLogin ">
    <table   width= "300 "   border= "0 "   align= "center ">
        <tr>
            <td   width= "98 "   height= "30 "> <div   align= "center "> 用户名: </div> </td>
            <td   width= "192 "> <input   name= "username "   type= "text "   id= "username "> </td>
        </tr>
        <tr>
            <td> <div   align= "center "> 密   &nbsp;码: </div> </td>
            <td   height= "30 "> <input   name= "password "   type= "password "   id= "password "> </td>
        </tr>
        <tr>
            <td   height= "30 "> &nbsp; </td>
            <td> <input   name= "Submit "   type= "submit "   value= "提交 ">
            &nbsp;
            <input   type= "reset "   name= "Submit2 "   value= "重置 "> </td>
        </tr>
        <tr>
            <td   height= "30 "> &nbsp; </td>
            <td> 新用户注册 </td>
        </tr>
        <tr>
            <td   height= "30 "> &nbsp; </td>
            <td> 后台管理 </td>
        </tr>
    </table>
</form>
<%
If   Request( "action ")= "checkLogin "   then
Call   checkLogin()
End   If
%>
<%
Sub   checkLogin()
Dim   sql,rs,username,password
username=trim(request( "username "))
password=trim(request( "password "))
If   username= " "   Then
response.write   " <script> window.alert( '请输入用户名! ');_history.back(); </script> "
Else
If   password= " "   Then
response.Write " <script> window.alert( '请输入密码! ');_history.back(); </script> "
Else
Set   rs   =   Server.CreateObject( "ADODB.Recordset ")
sql= "select   *   from   commonuser   where   username= ' "&username& " ' "


rs.Open   sql,conn,2,3
If   rs.eof   Then
Response.Write " <script> window.alert( '用户名不存在! ');_history.back(); </script> "
Response.End()
Else
If   password   <>   rs( "password ")   Then
Response.Write " <script> window.alert( '密码错误! ');_history.back(); </script> "
Response.End()
Else
Session( "admin ")=rs( "username ")
Response.Redirect   "index.asp "
End   If
End   If
End   If
End   If
rs.close
set   rs=nothing
End   Sub
%>
</body>
</html>
预览的时候用户名和密码都不输入的情况下,就报错如下
错误类型:
Microsoft   VBScript   运行时错误   (0x800A01A8)
缺少对象:   'rs '
/study/Login/login.asp,   第   69   行(第   69   行就是rs.Open)


[解决办法]
rs.close
set rs=nothing
放错位置了

....
rs.Open sql,conn,2,3
If rs.eof Then
Response.Write " <script> window.alert( '用户名不存在! ');_history.back(); </script> "
Response.End()
Else
If password <> rs( "password ") Then
Response.Write " <script> window.alert( '密码错误! ');_history.back(); </script> "
Response.End()
Else
Session( "admin ")=rs( "username ")
Response.Redirect "index.asp "
End If
End If
rs.close ' ' '应该放在这里才对
set rs=nothing

............


[解决办法]
哦,我看错了,下面这个应该可以了:
Sub checkLogin()
Dim sql,rs,username,password
username=trim(request( "username "))
password=trim(request( "password "))
If username= " " Then
response.write " <script> window.alert( '请输入用户名! ');_history.back(); </script> "
end if
If password= " " Then
response.Write " <script> window.alert( '请输入密码! ');_history.back(); </script> "
end if
Set rs = Server.CreateObject( "ADODB.Recordset ")
sql= "select * from commonuser where username= ' "&username& " ' "
rs.Open sql,conn,2,3
If rs.eof Then
Response.Write " <script> window.alert( '用户名不存在! ');_history.back(); </script> "
Response.End()
Else
If password <> rs( "password ") Then
Response.Write " <script> window.alert( '密码错误! ');_history.back(); </script> "
Response.End()
Else
Session( "admin ")=rs( "username ")
Response.Redirect "index.asp "
End If
End If
rs.close
set rs=nothing
End Sub

热点排行