【问题标题】:i got a error Invalid attempt to access a field before calling Read()我在调用 Read() 之前收到错误无效的访问字段尝试
【发布时间】:2014-08-08 11:40:35
【问题描述】:

我在这里尝试创建登录页面。 只有管​​理员可以访问此页面,当前管理员名称是 akshay。 登录页面cs代码:

string cnn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
        MySqlConnection con = new MySqlConnection(cnn);

        string IdText = UserName.Text;
        string PassText = Password.Text;

        MySqlCommand cmd = new MySqlCommand("select * from ab_db.login where user= @user and Pass = @Pass", con);
        con.Open();

        cmd.Parameters.Add("@user", MySqlDbType.String);
        cmd.Parameters["@user"].Value = UserName.Text;
        cmd.Parameters.Add("@Pass", MySqlDbType.String);
        cmd.Parameters["@Pass"].Value = Password.Text;

        MySqlDataReader dr = cmd.ExecuteReader();


        dr.Read();
        if (dr["user"].ToString().Trim() == IdText && dr["Pass"].ToString().Trim() == PassText)
        {
            Session["User"] = IdText;
            Response.Redirect("~/test2.aspx");
        }
        else
        {
            FailureText.Text = "Invalid Username or Password";
        }

        dr.Close();
        con.Close();

我想要安全的页面是 test2。 代码是:

 protected void Page_Load(object sender, EventArgs e)
{
    if (Session["User"] == "akshay")
    {
        if (!IsPostBack)
        {
            // Call FillGridView Method

            FillGridView();
        }
    }
    else
    {
        // No valid login...
        Session.Clear();
        Response.Redirect("~/myLogin.aspx");
    }


}

我遇到了一个我不明白为什么的错误?请帮忙。

错误是-

在调用 Read() 之前访问字段的尝试无效

【问题讨论】:

    标签: c# html asp.net authentication


    【解决方案1】:

    你应该检查Read()的结果

    if (dr.Read())
    {
        // 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多