Skip to content Skip to sidebar Skip to footer

Skipped One Error Checking In If Else Statement

I have this error checking in my program using the if else statment. I got 2 things to check. They are PoliceID(PK) NRIC The statement below checks if the textbox has the same v

Solution 1:

The Last one didn't work because of this query

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

                }
                elseif (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
{

                SqlConnectionconn=newSqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
                conn.Open();
                SqlCommandcmd=newSqlCommand("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";
}
}

To make your Existing code work try this query

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

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

Solution 2:

Your select statement seems to be the problem. It seems like you want nric to be unique too yet you don't use it in the where clause of your select statement. The way you have it now, as long as policeid is unique, any nric value will be fine. In other words, if first two checks pass, then the third one will too. Try this instead:

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

However if you don't want nric to be unique, then your code works fine!

Post a Comment for "Skipped One Error Checking In If Else Statement"