简体   繁体   English

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

[英]How to Eliminate Checking of Null Value Datatable in IF condition

In my Page I am fetching a value from the Database & filling the values in the DataTable. 在我的页面中,我正在从数据库中获取值,并将值填充到数据表中。 I am then comparing that values with the mac String in the IF. 然后,我将该值与IF中的mac String进行比较。 Based upon the condition in the Query there will be no records fetched, it is stuck in the IF condition and throws the No row at Position 0 Exception rather than going into the Else part. 根据查询中的条件,将不会提取任何记录,它会停留在IF条件中,并No row at Position 0异常No row at Position 0抛出No row at Position 0而不是进入Else部分。

My code is: 我的代码是:

  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();
            }
        }

First of all, you may want to give some more meaningful names to your variables. 首先,您可能想为变量指定一些更有意义的名称。

On a side note, you may want to change your for loops into a foreach loop: 附带说明一下,您可能需要将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...
    }
}

This way if there are no records fetched it won't go in. 这样,如果没有获取记录,它将不会进入。

After that, you may want to check the conditions on RowCount > 0 for the datatables before going inside the loops and act outside the loop if the RowCount is 0. 之后,您可能需要先检查数据表的RowCount > 0条件,然后再进入循环内部,如果RowCount为0,则在循环外部进行操作。

Just swap your OR condition in If statement: 只需在If语句中交换您的OR条件:

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

If you need to check against a null result I'd wrap my if statement in another if statement that does a simple check for a null result before doing anything else. 如果您需要检查是否为空结果,则可以将if语句包装在另一个if语句中,该语句在执行其他任何操作之前会先对null结果进行简单检查。 Something like this will check if it's not null: 这样的事情将检查它是否不为空:

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

You could add this into your current if statement check so: 您可以将其添加到当前的if语句中,如下所示:

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

becomes: 变成:

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

Though as Tallmaris says it might be better to restructure it using a foreach loop instead. 尽管正如Tallmaris所说,最好使用foreach循环来重组它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM