C#WPF加载用户在SQL中停止UNIQUE KEY约束 [英] C# WPF loading user stopping UNIQUE KEY constraint in SQL
问题描述
大家好,
我有一个C#WPF应用程序,我登录并将用户传递到下一个屏幕。然后我点击进入个人资料屏幕,如果用户已有个人资料数据,我自动填充所有字段。在我的个人资料屏幕中,我有它,所以当您将组合框设置为是时,它会在数据库中创建客户。单击保存按钮时,它只会更新所有数据。当我加载控件时,它将组合框设置为是,并尝试在数据库中重新创建客户。我收到一个错误:违反UNIQUE KEY约束。无法在DB中插入重复的密钥...我想检查用户是否已经存在于数据库中,如果这样做,它将不会创建新客户,因此不会抛出此错误。或任何其他方式来解决这个问题。不确定我是否应该检查这个特定的错误,只是console.log它或任何建议将不胜感激。
谢谢。
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()); }
}
}
}
我尝试过:
抛出错误
console.log
捕获特定异常
not not确定我应该做什么
What I have tried:
throwing error
console.log
catch specific exception
not sure what i should do
推荐答案
我知道 - 不幸的是你必须真正编写一些代码,但事情就是如此。
在试图插入新用户的代码周围放置一个try / catch块,并在catch部分中显示一个显示User name already exists。的消息框,让它成为那个。同时。
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.
这篇关于C#WPF加载用户在SQL中停止UNIQUE KEY约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!