Datareader有rows属性返回false

时间:2014-07-02 15:18:20

标签: asp.net

我正在尝试检查用户名是否存在。我在html页面中使用XMLHTTPREqUEST对象并将用户名和密码值发送到Default aspx页面。但是当我对代码进行exacute时。没有任何反应。我的意思是没有异常消息或任何调试后我注意到连接状态打开但是datareader的hasRows属性返回false。我确信我能够将查询字符串值传递给变量。我错过了什么你可以帮忙吗

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ka"] != null && Request.QueryString["pass"] != null)
        {

            string username = Request.QueryString["ka"];
            string pass = Request.QueryString["pass"];
            SqlConnection con = new SqlConnection("server=.\\sqlexpress;database=Projects;UID=sa;Password=1234");
            if (con.State == ConnectionState.Closed) {


                con.Open();
                SqlCommand cmd = new SqlCommand("select * from userstable where username=@name and pass=@pass", con);
                cmd.Parameters.AddWithValue("@name", username);
                cmd.Parameters.AddWithValue("@pass", pass);




                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {

                    Response.Write("exist");
                }
                else
                {

                    Response.Write("Doesntexist");
                }
                con.Close();




            }

        }


        Response.Close();

    }

这是Htmlpage

    <script src="Scripts/jquery-2.1.1.js"></script>
   <script type="text/javascript">


       function createXHR() {

           var xhr;


           try {
               xhr = new XMLHttpRequest();
           } catch (e) {

           }
           return xhr;
       }


       function signIn() {

           var xhr = createXHR();
           var username=$('#txtusername').val();
           var pass=$('#txtpass').val()

           xhr.onreadystatechange=function(){

               if(xhr!=null){

                   if(xhr.readyState==4){

                       if(xhr.status>=200 && xhr.status<300){

                           var v = $('#result').html(xhr.responseText);
                           if (v == "exist") {
                               $('#result').html("Done");
                           }
                           else if (v == "Doesntexist") {

                               $('#result').html("Error");
                           }
                           else{
                               $('#result').html(v);

                           }

                           }

                   }

               }
               else{
                   $('#result').html("Error");


               }
           }

           xhr.open("GET", "Default.aspx?ka='" + username + "&pass=" + pass, true);
           xhr.send(null);
       }
   </script>
</head>
<body>
    <input id="txtusername" type="text"  name="txtusername"/><br />
    <input id="txtpass" type="text"  name="txtpass"/><br />
    <input id="Button1" type="button" value="signin" onclick="signIn();" /><br />
    <div id="result" ></div>
</body>

0 个答案:

没有答案