如何在IF条件下消除Null值DataTable的检查 [英] How to Eliminate the Checking of the Null value DataTable in IF condition

查看:77
本文介绍了如何在IF条件下消除Null值DataTable的检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的页面中,我从数据库中获取一个值&填充DataTable中的值,并根据Query中的条件将这些值与IF中的String进行比较,将不会获取任何记录,因此它会陷入IF条件并抛出No position at Position 0Exception not not转到其他部分我的代码是,





Hi,

In my Page I am fetching a value from the Database & filling the values in the DataTable and comparing that values with the String in the IF based upon the condition in the Query there will be no records fetched, so It is stuck in the IF condition and throws the "No row at Position 0" Exception not going to the Else part 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();
               }
           }

推荐答案

您可以在for的开头添加if条件循环

请尝试以下代码





if(tab.Rows.Count> 0)

{



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

休息;

}

else

{

lblMessage.Visible = true;

lblMessage.ForeColor = System.Drawing.Color .Red;

lblMessage.Text =无效的用户名或密码!!!;



}

}

else

{

lblMessage.Visible = true;

lblMessage.ForeColor = System.Drawing.Color .Red;

lblMessage.Text =无效的评估接入点!!!;

}

}

}



}

}
You can add a if condition at the beginning of the for loop
please try the following code


if(tab.Rows.Count>0)
{

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 !!!";
}
}
}

}
}


这篇关于如何在IF条件下消除Null值DataTable的检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆