How to check if a value in a textbox already exists in a connected database
<p>So I am currently puzzled to the logic of my program, I have ran through it multiple times but I am not too sure. I basically want to be able to notify the user saying that the contact number they have entered already exists in the database. I have gotten close I am sure as I have done a lot of research but I do not think what I have done is actually checking the database but rather just telling it to match to anything that is in the textbox rather than checking the database.</p>

<p>Just to clarify <strong>I do not</strong> want to extract data from the database and put it into the textbox. Thanks.</p>

<p><strong>Update: Two different programs for two different suggested solutions.</strong></p>

<p><strong>(Possible Solution 1) Updated contact number is unique method using COUNT:</strong> </p>

<pre><code>private void CheckContactNumber()
{
    string checkContactNum = "SELECT COUNT(*) FROM Employee WHERE ContactNumber = " + addContactNum.Text + " "; //01234567890

    OleDbCommand cmd = new OleDbCommand(checkContactNum, conn);
    conn.Open();
    OleDbDataReader dr = cmd.ExecuteReader();

    if (dr.Read())
    {
        int countDup = (int)dr[0];

        if (countDup &gt; 0 &amp;&amp; addContactNum.Text != "")
        {
            err += "Contact number is already listed in the database\r\n";
            errorContactNum.Visible = true;
            uniqueContactNumber = false;
        }
        else if (countDup == 0 &amp;&amp; addContactNum.Text != "")
        {
            errorContactNum.Visible = false;
            uniqueContactNumber = true;
        }
    }

    conn.Close();
}
</code></pre>

<p><strong>(Possible Solution 2)Update to 2nd suggested solution:</strong></p>

<pre><code>if (err == "")
{
    // you miss s here
    string addEmployee = "if not exists(select LastName from Employee where LastName = @LastName)INSERT INTO Employee(FirstName, LastName, Role, DateOfHire, ContactNumber)" +"VALUES(@FirstName, @LastName, @Role, @DateOfHire, @ContactNumber)";

    OleDbCommand cmd = new OleDbCommand(addEmployee, conn);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = addFirstName.Text;
    cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = addLastName.Text;
    cmd.Parameters.Add("@Role", OleDbType.VarChar).Value = addRole.Text;
    cmd.Parameters.Add("@DateOfHire", OleDbType.VarChar).Value = addDateOfHire.Text;
    cmd.Parameters.Add("@ContactNumber", OleDbType.VarChar).Value = addContactNum.Text;

    conn.Open();

    // i removed the duplicated code
    if (cmd.ExecuteNonQuery() != 1)
    {
        err += "Contact number is already listed in the database\r\n";
                errorContactNum.Visible = true;
    }

    conn.Close();

    addFirstName.Text = String.Empty;
    addLastName.Text = String.Empty;
    addRole.Text = String.Empty;
    addContactNum.Text = String.Empty;
    addRole.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    }
    //Save It
    else
    {
        MessageBox.Show(err);
    }
}
</code></pre>

<p><a href="http://i.stack.imgur.com/nst4K.png" rel="nofollow">So this would be acceptable for adding to my database as all criterion is met but it doesnt accept it, I'm assuming it runs the else code right at the bottom of <strong>AddEmployee method</strong></a></p>

<p><a href="http://i.stack.imgur.com/9X2MJ.png" rel="nofollow">Here is an Image of my database which is linked to my program.</a></p>