跳过了一个错误签入if-else语句

本文关键字:错误 if-else 语句 一个 过了 | 更新日期: 2023-09-27 18:25:13

我在程序中使用if-else语句进行错误检查。我有两件事要检查。它们是

  1. PoliceID(PK)

  2. NRIC

下面的语句检查文本框中PoliceID和NRIC的值是否与数据库中的值相同。

if (tbpid.Text.Equals(dr["policeid"].ToString().Trim()) && (tbnric.Text.Equals(dr["nric"].ToString().Trim())))
                {
                    lbmsg.Text = "This police account has already exist. Please verify the details again.";
                }

如果文本框(警察id)的值与数据库中的值相同,则它们将给出另一个不同的错误。

 if (tbpid.Text.Equals(dr["policeid"].ToString()))
                {
                    lbmsg.Text = "This police ID has already exists. Please generate another Police ID";
                }

如果文本框(NRIC)的值与数据库中的值相同,他们将给出另一个错误

if (tbnric.Text.Equals(dr["nric"].ToString()))
                {
                    lbmsg.Text  ="This NRIC has already exist. Please ensure that the NRIC is correct";
                }

如果我将所有的错误检查消息组合在一起,它将是这样的。

 protected void btnAdd_Click(object sender, EventArgs e)
    {

            SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
            con.Open();
            SqlCommand select = new SqlCommand("Select policeid, nric from PoliceAccount where policeid = @policeid" , con);
            SqlDataReader dr;
            select.Parameters.AddWithValue("@policeid", tbpid.Text);            
            dr = select.ExecuteReader();
            if(dr.Read())
            {
                if (tbpid.Text.Equals(dr["policeid"].ToString().Trim()) && (tbnric.Text.Equals(dr["nric"].ToString().Trim())))
                {
                    lbmsg.Text = "This police account has already exist. Please verify the details again.";
                }
                else if (tbpid.Text.Equals(dr["policeid"].ToString()))
                {
                    lbmsg.Text = "This police ID has already exists. Please generate another Police ID";
                }
                else if (tbnric.Text.Equals(dr["nric"].ToString()))
                {
                    lbmsg.Text  ="This NRIC has already exist. Please ensure that the NRIC is correct";
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
                conn.Open();
                SqlCommand cmd = new SqlCommand("insert into PoliceAccount(policeid, password, nric, fullname, postedto)  values('" + tbpid.Text.Trim() + "','" + tbpid.Text.Trim() + "','" + tbnric.Text.Trim() + "','" + tbfullname.Text.Trim() + "', '" + ddllocation.SelectedValue + "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                lbmsg.Text = "Congratulations. The police account of ID " + tbpid.Text + " has been successfully added. You may edit the profile via the edit profile tab above";
                tbpid.Text = "";
                tbnric.Text = "";
                tbfullname.Text = "";
                ddllocation.SelectedValue = "Select Location";

            }
            //ConfirmButtonExtender2.ConfirmText = "Are you sure you want to add this Police Account " + tbpid.Text + " ?";
        }
    }

然而,这里的问题是,错误检查消息的前两条语句成功地工作了。不幸的是,NRIC的一个没有起作用。例如,如果我要键入不同的策略ID但相同的NRIC,则数据仍在插入数据库中,这意味着它完全忽略上面的NRIC错误检查。我已经看了好几个小时了,还没有发现问题。如果有人能指导我,我将不胜感激。

此外,在我的数据库中,我已将主键设置为策略ID,而NRIC只是我的数据库中的常规数据列

谨致问候。

跳过了一个错误签入if-else语句

由于这个查询,最后一个没有工作

Select policeid, nric from PoliceAccount where policeid=@policeid这不会返回任何行,因为ploiceid不存在意味着dr.Read()为false,因为没有行可读取,所以它直接进入数据库中插入数据的其他部分。所以让它发挥作用。试着这样。。。

 if(dr.Read())
            {
                if (tbpid.Text.Equals(dr["policeid"].ToString().Trim()) && (tbnric.Text.Equals(dr["nric"].ToString().Trim())))
                {
                    lbmsg.Text = "This police account has already exist. Please verify the details again.";
                }
                else if (tbpid.Text.Equals(dr["policeid"].ToString()))
                {
                    lbmsg.Text = "This police ID has already exists. Please generate another Police ID";
                }
            }
            else
            {
 if (tbnric.Text.Equals(dr["nric"].ToString()))
                {
                    lbmsg.Text  ="This NRIC has already exist. Please ensure that the NRIC is correct";
                }
else
{
                SqlConnection conn = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
                conn.Open();
                SqlCommand cmd = new SqlCommand("insert into PoliceAccount(policeid, password, nric, fullname, postedto)  values('" + tbpid.Text.Trim() + "','" + tbpid.Text.Trim() + "','" + tbnric.Text.Trim() + "','" + tbfullname.Text.Trim() + "', '" + ddllocation.SelectedValue + "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                lbmsg.Text = "Congratulations. The police account of ID " + tbpid.Text + " has been successfully added. You may edit the profile via the edit profile tab above";
                tbpid.Text = "";
                tbnric.Text = "";
                tbfullname.Text = "";
                ddllocation.SelectedValue = "Select Location";
}
}

要使现有代码工作,请尝试此查询

Select policeid, nric from PoliceAccount where policeid=@policeid or nric=@nric

如果其中一个id存在于数据库中,则它将始终返回行,如果两者都不存在,则它插入数据库。

您的select语句似乎是个问题。你似乎也希望nric是唯一的,但你没有在select语句的where子句中使用它。按照现在的方式,只要policeid是唯一的,任何nric值都可以。换句话说,如果前两次检查通过,那么第三次也会通过。试试这个:

SqlCommand select = new SqlCommand("Select policeid, nric from PoliceAccount where policeid = @policeid or nric = @nric" , con);
SqlDataReader dr;
select.Parameters.AddWithValue("@policeid", tbpid.Text);
select.Parameters.AddWithValue("@nric", tbnric.Text);
dr = select.ExecuteReader();

但是,如果您不希望nric是唯一的,那么您的代码可以正常工作!