Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.
C#
//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()); }
                }
            }
        }


What I have tried:

throwing error
console.log
catch specific exception
not sure what i should do
Posted
Updated 1-Dec-18 4:47am
v2
Comments
Graeme_Grant 30-Nov-18 21:18pm    
Do you understand the error that you are seeing?
TheBigBearNow 30-Nov-18 21:42pm    
Yes im getting a null reference error about can not enter duplicate unique id into the database.
Im trying a couple different ideas i have and if that dont work i might try the boolean (flag) idea you put on my other post because that may work on this same problem i am having i could set the flag in the constructor and i could just skip the addCustomer if my database field is set to true.

1 solution

I know - it's unfortunate that you have to actually write some code, but that's how it goes.

Put a try/catch block around the code that's attempting to insert the new user, and in the catch part, show a message box that shows "User name already exists.", and let that be that. At the same time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900