0

在我的页面中,我从数据库中获取一个值并填充数据表中的值。然后我将该值与macIF 中的字符串进行比较。根据 Query 中的条件,将不会获取任何记录,它会卡在 IF 条件中并抛出No row at Position 0Exception 而不是进入 Else 部分。

我的代码是:

  string mac = GetMac();
        string Qry = "Select VUserid,Password from passtable where VUserid='" + UserName.Text + "' and Flag='A'";
        string qry = "Select VUserid,Password from passtable where Flag='A'";
        string strq = "Select Mac_id from Sysinfo Where Appflag='A'";
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EvalCon"].ConnectionString))
        {
            try
            {

                SqlCommand cmd = new SqlCommand(Qry, conn);
                SqlCommand cmd1 = new SqlCommand(qry, conn);
                SqlCommand cmd2 = new SqlCommand(strq, conn);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                SqlDataAdapter dap = new SqlDataAdapter(cmd2);
                DataTable dt = new DataTable();
                DataTable dtt = new DataTable();
                DataTable tab = new DataTable();
                da.Fill(dt);
                daa.Fill(dtt);
                dap.Fill(tab);
                for (int i = 0; i < tab.Rows.Count; i++)
                {
                    for (int x = 0; x <= dtt.Rows.Count - 1; x++)
                    {
                        if (mac == tab.Rows[i]["Mac_id"].ToString() || tab.Rows.Count != 0)
                        {
                            if (UserName.Text == dtt.Rows[x]["VUserid"].ToString() && Password.Text == dtt.Rows[x]["Password"].ToString())
                            {
                                Response.Redirect("~/Changepass.aspx");
                                break;
                            }
                            else
                            {
                                lblMessage.Visible = true;
                                lblMessage.ForeColor = System.Drawing.Color.Red;
                                lblMessage.Text = "Invalid Username or Password !!!";

                            }
                        }
                        else
                        {
                            lblMessage.Visible = true;
                            lblMessage.ForeColor = System.Drawing.Color.Red;
                            lblMessage.Text = "Invalid Access Point for Evaluation !!!";
                        }
                    }
                }

            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
4

3 回答 3

0

首先,您可能希望为变量提供一些更有意义的名称。

附带说明一下,您可能希望将 for 循环更改为 foreach 循环:

foreach (DataRow tabRow in tab.Rows.Count)
{
    foreach (DataRow dttRow in dtt.Rows.Count)
    {
        // logic here
        // tab.Rows[i]["Mac_id"] becomes tabRow["Mac_id"]
        // and
        // dtt.Rows[x]["VUserid"] becomes dttRow["VUserid"]
        // and so on...
    }
}

这样,如果没有获取记录,它就不会进入。

之后,您可能希望RowCount > 0在进入循环之前检查数据表的条件,如果 RowCount 为 0,则在循环外执行操作。

于 2013-05-15T08:13:39.483 回答
0

如果您需要检查空结果,我会将 if 语句包装在另一个 if 语句中,该语句在执行其他任何操作之前对空结果进行简单检查。像这样的东西会检查它是否不为空:

if(tab.rows[i]["Mac_id"] != null)
{
//logic here
}

您可以将其添加到当前的 if 语句检查中:

if(mac == tab.Rows[i]["Mac_id"].ToString() || tab.Rows.Count != 0)

变成:

if(mac == tab.Rows[i]["Mac_id"].ToString() || tab.Rows.Count != 0 && tab.rows[i]["Mac_id"] != null)

尽管正如 Tallmaris 所说,使用 foreach 循环来重构它可能会更好。

于 2013-05-15T08:25:53.320 回答
0

只需在 If 语句中交换您的 OR 条件:

if (tab.Rows.Count != 0 || mac == tab.Rows[i]["Mac_id"].ToString())
{
    ...
    ...
}
于 2013-05-15T08:19:59.493 回答