Quick Rundown

VS2008; MS SQL 2005

I'll post my code below my explanation.

I have a form within my project bound to a table called Company which has a key of CompanyID. I used the designer to create the data source and hook up the controls. I also used insert, update, and delete stored procedures, once again using the designer. In a normal windows forms environment all is well. All procedures are working and the data is inserted, updated, and deleted like it should. The bindingnavigator also works as designed.

What I'm trying to do: When a user inputs a value into the main field/control of my form, which is the CompanyName field, and tabs off the control I'd like to populate the form with that value if it matches a record in the dataset or prompt the user to insert a new record if I don't get an exact match. This is meant to avoid getting the unique error back from the database (the field is set to unique).

So I was thinking of using a leave event and then do a bindingsource.filter with criteria. If I get a match, pull up the record. If not, ask the user if they would like to create it - using a messagebox. Then go into bindingsource.addnew etc. Or is my way of thinking off here?

Here's the code:
Code:
private void txtName_Leave(object sender, EventArgs e)
        {
            // The name must match exactly on the filter.
            this.companyBindingSource.Filter = "Name LIKE '" + txtName.Text + "'";
            if (companyBindingSource.Count == 0) 
            {
                companyBindingSource.RemoveFilter();
                companyBindingSource.AddNew();
                
                txtAddress1.Focus();
            }
My thoughts lean towards business logic. If a user enters a string that doesn't exactly match a name I already have in the underlying data they should be prompted to add it or decline.

When I get a match on the filter everything is great and the record populates the form. When I don't, I just want to go with the add new, but if I have to tab by that textbox again the leave event fires again.

How do I accomplish what seems like such a simple task - if a value doesn't exist get out of the leave event and just add a new record.

I appreciate anyone that read this and takes the time to help me.