当前位置: 代码迷 >> .NET组件控件 >> 读取器疏失
  详细解决方案

读取器疏失

热度:1454   发布时间:2013-02-25 00:00:00.0
读取器出错
自己做了一个登陆验证控件,大体上能验证通过,只不过还有一点异常不知道在哪里
下边是我的代码,主要是看红色部分
 try
        {
            string constr = ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
            string sql = "select username from userlogin where id='" + TxtUserName.Text.Trim() + "' and password='" + TxtUserPwd.Text.Trim() + "'";
            SqlConnection connection = new SqlConnection(constr);
            //SqlCommand command = new SqlCommand(sql, connection);
            connection.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, connection); //创建适配器
            DataSet ds = new DataSet(); //创建数据集
            int count = da.Fill(ds, "table");
            if (count > 0)
            {
                SqlCommand command = new SqlCommand(sql, connection);
                SqlDataReader dr = command.ExecuteReader();
                dr.Read();
                Session["user"] = dr["username"].ToString();
                dr.Close();
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                Label2.Text = "登录失败";
            }
            connection.Close();
       }
       catch
        {
            Response.Redirect("~/web/errormessage/loginerror.aspx");
        }
当使用合法用户登录时,可以登陆进去,但是会跳转到catch语句里的界面,而输入错误用户信息时只显示“登陆失败”,不会跳转到catch里的页面,所以我想应该是红色部分出错了,哪位可以指点一下
------解决方案--------------------------------------------------------
你确认reader中能读到内容吗,你还是调试一下吧
------解决方案--------------------------------------------------------
第一次看见有人这么做验证。。。太雷
------解决方案--------------------------------------------------------
int count = da.Fill(ds, "table");有问题吧
------解决方案--------------------------------------------------------
引用:
  相关解决方案