如何在IF条件下消除对空值数据表的检查

本文关键字:空值 数据表 检查 IF 条件下 | 更新日期: 2023-09-27 18:13:32

在我的页面,我从数据库&填充DataTable中的值。然后,我将这些值与IF中的mac字符串进行比较。根据Query中的条件,将不会获取任何记录,它被卡在IF条件中并抛出No row at Position 0异常,而不是进入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();
            }
        }

如何在IF条件下消除对空值数据表的检查

首先,你可能想给你的变量起一些更有意义的名字。

另外,你可能想把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,则在循环外执行。

在If语句中交换OR条件:

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

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

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循环来重构它可能会更好。