我在程序中使用if else语句检查此错误。我有两件事需要检查。他们是
PoliceID(PK)
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.";
}
如果文本框(police 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的一个没有用。例如,如果我要键入不同的policeID但是相同的NRIC,则数据仍然被插入到数据库中,这意味着它完全忽略了上面的NRIC错误检查。我已经看了几个小时,我还没有找到问题。如果有人能指导我,我将不胜感激。
要添加,在我的数据库中,我已将主键设置为 policeID ,而 NRIC 只是常规我的数据库中的数据列
问候。
答案 0 :(得分:1)
由于此查询
,最后一个无法正常工作 Select policeid, nric from PoliceAccount where policeid=@policeid
这不会返回任何行,因为ploiceid不存在意味着dr.Read()
是假的,因为没有要读取的行,所以它直接进入你在数据库中插入数据的else部分。所以要使它工作。试试这个......
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存在于数据库中,它将始终返回行,如果两者都没有插入到数据库中。
答案 1 :(得分:1)
您的选择语句似乎是个问题。看起来您希望nric也是唯一的,但您不会在select语句的where子句中使用它。你现在的方式,只要警察是独一无二的,任何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是唯一的,那么你的代码工作正常!