检查记录是否存在可防止编辑后保存

本文关键字:编辑 保存 可防止 存在 记录 是否 检查 | 更新日期: 2023-09-27 18:24:29

我有一段代码,在将数据插入数据库之前,它会检查数据的出现情况。我的问题是,在编辑数据库中的现有数据后,它会不断显示它已经存在的消息。有人能帮我提供一个解决方案,防止它在编辑后阻止我输入数据吗?下面是我的C#代码:

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    if (txtFullNames.Text.Length == 0)
    {
    Errormessage.Text = "Please enter your fullname";
    txtFullNames.Focus();
    }
    else if(!Regex.IsMatch(txtFullNames.Text, @"^[a-zA-Z]"))
    {
    Errormessage.Text = "Fullname must not contain a number";
    }
    else if (txtSurname.Text.Length == 0)
    {
    Errormessage.Text = "Please enter your Surname";
    txtSurname.Focus();
    }
    else if (!Regex.IsMatch(txtSurname.Text, @"^[a-zA-Z]"))
    {
    Errormessage.Text = "Surname must not contain a number";
    }
    else if (txtEmail.Text.Length == 0)
    {
    Errormessage.Text = "Please enter your Email Address";
    txtEmail.Focus();
    }
    else if (!Regex.IsMatch(txtEmail.Text, @"^[a-zA-Z]['w'.-]*[a-zA-Z0-9]@[a-zA-Z0-9]['w'.-]*[a-
zA-Z0-9]'.[a-zA-Z][a-zA-Z'.]*[a-zA-Z]$"))
    {
    Errormessage.Text = "Please enter you valid email address";
    txtEmail.Focus();
    }
    else if (cboGender.SelectedIndex == 0)
    {
    Errormessage.Text = "Please select your Gender";
    cboGender.Focus();
    }
    else if (cboCompany.SelectedIndex == 0)
    {
    Errormessage.Text = "Please select your Company";
    cboCompany.Focus();
    }
    else if (txtUserName.Text.Length == 0)
    {
    Errormessage.Text = "Please enter your Username";
    txtUserName.Focus();
    }
    else if (txtPassword.Password.Length == 0)
    {
    Errormessage.Text = "Please enter your password";
    txtPassword.Focus();
    }
    else if (txtConfirmPassword.Password.Length == 0)
    {
    Errormessage.Text = "Please confirm your password";
    txtConfirmPassword.Focus();
    }
    else if (txtPassword.Password != txtConfirmPassword.Password)
    {
    Errormessage.Text = "Both password must match";
    }
    else
    {
    SqlConnection oConnection = new SqlConnection(_ConnectionString);
    SqlCommand Check_Exist = new SqlCommand("SELECT UserName from UserEnrollment WHERE username = 
@UserName AND Password = @Password", oConnection);`enter code here`
    Check_Exist.Parameters.AddWithValue("@UserName", txtUserName.Text);
    Check_Exist.Parameters.AddWithValue("@Password", txtPassword.Password);
    oConnection.Open();
    SqlDataReader reader = Check_Exist.ExecuteReader();
    if (reader.HasRows)
    {
    MessageBox.Show("Username and Password already exist!", "Error Message", 
MessageBoxButton.OKCancel, MessageBoxImage.Error);
    }
    else
    {
    int Gender = 0;
    bool passed = int.TryParse(cboGender.SelectedValue.ToString(), out Gender);
    int Company = 0;
    passed = int.TryParse(cboCompany.SelectedValue.ToString(), out Company);
    if (passed)
    {
    try
    {
    switch (g_i_commitType)
    {
    case 1:
    {
    bool _var = _da.InsertData(UserID,
    txtUserName.Text,
    txtPassword.Password,
    txtFullNames.Text,
    txtSurname.Text,
    txtEmail.Text,
    Gender,
    Company
    );
    } break;
    case 2:
    {
    bool _var = _da.EditData(UserId,
    txtUserName.Text,
    txtPassword.Password,
    txtFullNames.Text,
    txtSurname.Text,
    txtEmail.Text,
    Gender,
    Company
    );
    } break;
    }
    LoadGridUsers();
    MessageBox.Show("Data Successfully Submited", "Confirmation Message");
    DeleteTextBox();
    Errormessage.Text = "";
    }
    catch (Exception ex)
    {
    MessageBox.Show("A handled exception occurred" + ex.Message, "Error Message", 
MessageBoxButton.OK, MessageBoxImage.Error);
    }
    }
    }

我不确定我的逻辑会阻止我在编辑后保存。

检查记录是否存在可防止编辑后保存

将您的"检查"代码放入if语句:if(g_i_commitType == 1)中,因为这是视图的"添加"模式,仅对于g_i_commitType == 1,您应该检查用户是否已经存在。

// all you validation if statements
else
{
    if(g_i_commitType == 1)
    {
        SqlConnection oConnection = new SqlConnection(_ConnectionString);
        SqlCommand Check_Exist = new SqlCommand("SELECT UserName from UserEnrollment WHERE username = @UserName AND Password = @Password", oConnection);`enter code here`
        Check_Exist.Parameters.AddWithValue("@UserName", txtUserName.Text);
        Check_Exist.Parameters.AddWithValue("@Password", txtPassword.Password);
        oConnection.Open();
        SqlDataReader reader = Check_Exist.ExecuteReader();
        if (reader.HasRows)
        {
            MessageBox.Show("Username and Password already exist!", "Error Message", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            return;
        }
    }
    int Gender = 0;
    // rest of you code