跳过如果else语句有一个错误检查 [英] Skipped one error checking in if else statement

查看:96
本文介绍了跳过如果else语句有一个错误检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用的if else说明书在我的计划这个错误检查。我有两件事情要检查。他们是

I have this error checking in my program using the if else statment. I got 2 things to check. They are


  1. PoliceID(PK)

  1. PoliceID(PK)

身份证号码

下面检查语句如果文本框有PoliceID和身份证在数据库相同的值。

The statement below checks if the textbox has the same value for PoliceID and NRIC as in the database.

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 the textbox (police id) has the same value as in the database, they will give another different error.

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

如果文本框(身份证)具有相同的值在数据库中,它们将给出另一个错误

If the textbox (NRIC) has the same value as in the database, they will give another error

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

如果我是所有错误校验信息结合在一起将是这个样子。

and if i were to combine all error checking message together it will be like this.

 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 + " ?";
        }

    }

然而这里的问题是,该错误检查信息的头两个语句管理工作。不幸的是,身份证一个没工作。例如,如果我是键入一个不同的policeID但相同的身份证,数据仍然插入,这意味着它完全忽略上面的身份证号码错误校验数据库。我一直在寻找这几个小时,我还没有发现问题。将AP preciate如果任何人都可以指导我在此。

However the problem here is, the first two statement of the error checking message managed to work. Unfortunately, the NRIC one did not work. For example, if i were to type a different policeID but the same NRIC, the data is still being inserted into the database which means it totally ignore the NRIC error check above. I have been looking at it for hours and i have yet to find the problem. Would appreciate if anyone can guide me on this.

要添加的,在我的数据库,我已经把我的主键作为 policeID ,而身份证号码只是一个常规数据列在我的数据库

To add on, in my database, i have set my primary key as the policeID while NRIC is just a regular data column in my database

问候。

推荐答案

由于此查询的最后一个没有工作

The Last one didn't work because of this query

选择policeid,从PoliceAccount身份证,其中policeid = @ p​​oliceid
因为ploiceid不存在这样的手段不会返回任何行 dr.Read()是假的,因为没有行字,所以它会直接在其他部分,你要插入在数据库中的数据。因此,要使它发挥作用。尝试像这样...

Select policeid, nric from PoliceAccount where policeid=@policeid This will not return any row since ploiceid doesn't exist means dr.Read() is false since no rows to read , so it goes directly in else part where you are inserting data in database . So to make it work. try like this...

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

为了使现有的code的工作尝试此查询

To make your Existing code work try this query

选择policeid,从PoliceAccount身份证,其中policeid = @ p​​oliceid或身份证= @身份证

它总是会返回行,如果在数据库中存在的一个ID,如果两人都没有比它插入到数据库中。

It will always return rows if one of the id exist in database if both didn't than it insert in to database.

这篇关于跳过如果else语句有一个错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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