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

时间:2014-09-25 09:05:09

标签: c# wpf

我有代码,可以在将数据插入数据库之前检查数据是否出现。我的问题是,在从数据库编辑现有数据后,它会继续显示已存在的消息。有人可以帮我一个解决方案,以防止它在编辑后阻止我输入数据吗?以下是我的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);

    }
    }
    }

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

1 个答案:

答案 0 :(得分:0)

把你的"检查"将代码编入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