Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

c# - Prevent a button to continue when there are an errors

How do i prevent a button to continue when there are an errors appears?

I already can check the availability of username in database, but even though the username is not exists in the database, the "Check Availability" button still recognized it as exists.

Here is the screenshots: enter image description here

Image above show the username "Seranne" already exists, but in the database, Seranne is not exists.

enter image description here

Here is the code:

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:WindowsMediaWindows 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=ArchivesProjectsProgramSell SystemSell SystemApp_Datadb1.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:WindowsMediaWindows 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:WindowsMediaWindows Notify.wav");
                sound.Play();
                MessageBox.Show("Username is not exists!", "Congratulations");
                return true;
            }
        }

edit: even though the username is not exists in the database, "Check Availability" button still recognized it as exists and this is the reason that i can't proceed.

How do i solve this? Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

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

  //save call
}

You can add a return value in CheckUserName and break out if it return false


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...