出现错误时阻止按钮继续

时间:2013-08-25 05:43:49

标签: c# winforms ms-access-2010

如果出现错误,如何阻止按钮继续?

我已经可以检查数据库中用户名的可用性,但即使数据库中不存在用户名,“检查可用性”按钮仍然会将其识别为存在。

以下是截图: enter image description here

上面的图片显示用户名“Seranne”已经存在,但在数据库中,Seranne不存在。

enter image description here

以下是代码:

else if (textBox1.Text.Length > 0 && textBox2.Text == textBox3.Text)
            {
                label5.Visible = false;
                label7.Visible = false;

                conn.Open();

                CheckUsername();

                if (CheckUsername() == false)
                {
                    return;
                }

                cmd.CommandText = "INSERT INTO [Member] ([Username], [Password], [UserType]) VALUES (@Username, @Password, @UserType)";

                cmd.Parameters.Add("Username", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["Username"].Value = this.textBox1.Text;

                cmd.Parameters.Add("Password", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["Password"].Value = this.textBox2.Text;

                cmd.Parameters.Add("UserType", System.Data.OleDb.OleDbType.VarChar);
                cmd.Parameters["UserType"].Value = this.comboBox1.Text;

                int numberOfRows = cmd.ExecuteNonQuery();

                conn.Close();

                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sound.Play();
                var dialogresult = MessageBox.Show("Your username and password has been recorded", "Congratulations", MessageBoxButtons.OK);

                CreateTable();

                if (dialogresult == DialogResult.OK)
                {
                   this.Hide();

                   Form1 form = new Form1();
                   form.ShowDialog();

                   this.Close();
                }

private void CheckUsername()
        {
            OleDbConnection conn = new OleDbConnection();

            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb";

            conn.Open();

            OleDbCommand cmd = new OleDbCommand("SELECT [Username] FROM [Member]", conn);

            cmd.Parameters.Add("Username", System.Data.OleDb.OleDbType.VarChar);
            cmd.Parameters["Username"].Value = this.textBox1.Text;

            OleDbDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sound.Play();
                MessageBox.Show("Username already exists! Please use another username", "Warning");
                return false;
            }

            else
            {
                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sound.Play();
                MessageBox.Show("Username is not exists!", "Congratulations");
                return true;
            }
        }

编辑:即使数据库中不存在用户名,“检查可用性”按钮仍然认为它存在,这就是我无法继续的原因。

我该如何解决这个问题? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

private bool CheckUsername()
{
  // return false if user name exists, otherwise tru
}
button_click(..)
{
  if(CheckUsername() == false)
  { 
     MessageBox.Show(Error Msg);
     return;
  }

  //save call
}

您可以在CheckUserName中添加返回值,如果返回false则退出