【问题标题】:IndexOutOfRange Exception while accessing two columns from an access database从访问数据库访问两列时出现 IndexOutOfRange 异常
【发布时间】:2013-05-07 10:52:40
【问题描述】:

while (reader.Read())
{
    if (TextBox1.Text.CompareTo(reader["usernam"].ToString()) == 0&&TextBox2.Text.CompareTo(reader["passwd"].ToString()) == 0) // A little messy but does the job to compare your infos assuming your using a textbox for username and password
    {
        Label3.Text = "Redirecting";



        Response.Cookies["dbname"]["Name"] = reader["usernam"].ToString();
        Response.Cookies["dbname"].Expires = DateTime.Now.AddSeconds(10);
        Response.Redirect("index2.aspx");

    }
    else
    { Label3.Text = "NO"; }

}

当我尝试比较用户名 (usernam) 和密码 (passwd) 时,我收到此错误。 如果我只将用户名与数据库条目进行比较,它就像一个魅力。

只有在使用实际数据时才会报错。 EI如果我在登录网页中输入 [admin], [admin] 它会给我错误,如果我输入 [asd], [asd] 那么标签将变为 NO。

代码背后的想法是一个登录页面。 我希望我的解释足够好。

【问题讨论】:

  • 请将实际代码添加到您的问题中作为文本

标签: c# asp.net vb.net ms-access


【解决方案1】:

您只是从表中选择用户名。您没有选择密码,因此当您尝试从结果集中检索密码时会引发异常。

将查询更改为:

string selectString = "SELECT usernam, passwd FROM Table1";

【讨论】:

    【解决方案2】:

    您也可以在查询中使用 * 代替列名。这里只是一个小场景。如果有多个列,那么您只需要使用 * 即可。这将使您的查询变得简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多