Prevent Duplicate record insert in database if record exists in WPF Application using C# and VB.Net

Last Reply on Dec 02, 2018 11:28 PM By dharmendr

Posted on Nov 30, 2018 06:40 PM

Hello all,

I have a C# WPF application that I log into and I pass the user to the next screen. Then I click to go to a profile screen and if the user already has profile data I autopopulate all the fields. In my profile screen I have it so when you set the combobox to ‘Yes’ it creates the customer in the database. When you click the save button it just updates all the data. When I load the controls it sets the combobox to ‘Yes’ and it tries to recreate the customer in the DB. I get a error: “Violation of UNIQUE KEY constraint. Cannot insert duplicate key into DB…” I would like to check if the user already exists in the DB and if it does so it WILL NOT create a new customer so this error will not be thrown. Or any other way to get around this. Not sure if I should check for this specific error and just console.log it or any suggestions would be appreciated.

Thank you. 

        //combobox selectionchanged when ‘yes’ add new customer        					
        private void CboCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string cboValue = "";
            if (CboCustomer.SelectedIndex > 0)
                cboValue = ((ComboBoxItem)CboCustomer.SelectedItem).Content.ToString();

            if(cboValue.Equals("Yes")) // || if (CboCustomer.SelectedItem.ToString().Equals("Yes"))
            {
                boolIsCustomer = true;
                User addCustomer = null;
                Customer newCustomer = null;

                //MessageBoxResult result = MessageBox.Show("Updating database to customer status.",
                //    "Customer Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                //if (result == MessageBoxResult.Yes)
                //{
                if (currentUser.IsCustomer == true)
                {
                    try
                    {                          
                        Customer custTestID = UsersDB.ReadCustomerById(currentUser.UserID);

                        if(currentUser.UserID == custTestID.UserID)
                        {
                            return;
                        }
                        else
                        {
                            addCustomer = new User(currentUser.UserID, currentUser.Username,
                            currentUser.Password, currentUser.IsAdmin, currentUser.UserCreatedDate, boolIsCustomer);
                            UsersDB.UpdateCurrentUser(addCustomer);

                            newCustomer = new Customer(currentUser.UserID, currentUser.Username, TextboxLastName.Text,
                                                    TextboxAddress.Text, TextboxCity.Text, null, 
                                                    TextboxZip.Text, TextEmailAddress.Text);
                            UsersDB.CreateCustomer(newCustomer);
                        }
                    }
                    catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); }
                }
                else
                {
                    try
                    {   //UPDATE User so 'IsCustomer' property is set to True.
                        addCustomer = new User(currentUser.UserID, currentUser.Username, currentUser.Password,
                                                currentUser.IsAdmin, currentUser.UserCreatedDate, boolIsCustomer);
                        UsersDB.UpdateCurrentUser(addCustomer);
                        //CREATE Customer from current User linking together by 'UserId'
                        newCustomer = new Customer(currentUser.UserID, currentUser.Username, TextboxLastName.Text,
                                                    TextboxAddress.Text, TextboxCity.Text, null, TextboxZip.Text,
                                                    TextEmailAddress.Text);
                        UsersDB.CreateCustomer(newCustomer);
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
                }
            }
        }

 

This question does not have replies that have been liked.