当我尝试访问查询结果时,出现“索引超出数组边界”错误

时间:2016-08-19 16:01:23

标签: c#

[正如标题所说......每当我尝试点击登录时弹出。只是 enter image description here

我还有另一个系统使用相同的系统,但它没有像这样的错误,而且现在我太盲目无法搜索错误的XDD

private void btnLogin_Click(object sender, EventArgs e)
{
  try
  {
    string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
    |DataDirectory|\MMG.mdb";
    string cmdText = ("SELECT COUNT(*) FROM Accounts WHERE User_name=? AND Pass_word=?");
    using (OleDbConnection con = new OleDbConnection(constring))
    using (OleDbCommand cmd = new OleDbCommand(cmdText, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@UName", txtUserName.Text);
        cmd.Parameters.AddWithValue("@PWord", txtPassword.Text);
        DataTable dt = db.execQuery("SELECT * FROM Accounts WHERE User_name='" + txtUserName.Text + "' AND Pass_word='" + txtPassword.Text + "'");

        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0].ItemArray[5].ToString() == "Manager")
            {
                this.Hide();
                frmMainForm mnf = new frmMainForm();
                mnf.ShowDialog();
                this.Close();
            }
            else if (dt.Rows[0].ItemArray[5].ToString() == "Cashier")
            {
                this.Hide();
                Sales mnf = new Sales();
                mnf.ShowDialog();
                this.Close();
            }
        }
    }
}

提前感谢您的解决方案! :)

1 个答案:

答案 0 :(得分:1)

您需要检查ItemArray:

if(dt.Rows.Count > 0)
{
   if(dt.Rows[0].ItemArray.Length > 5)
   {
      // YOUR CODE
   }
}
相关问题