Saturday, 24 August 2013

Prevent a button to continue when there are an errors

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 when the
messageBox is appear and says: "The username is exists!", and when i click
a submit button, the program still continue and ignored the error.
Here is the screenshots:
Image above show the username "Seranne" already exists, but when i click
"Submit" or "Register" button, it still continue and record the data to
the database like image shown below.

Here is the code:
else if (textBox1.Text.Length > 0 && textBox2.Text == textBox3.Text)
{
label5.Visible = false;
label7.Visible = false;
conn.Open();
CheckUsername();
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();
}
How do i solve this? Thanks in advance!

No comments:

Post a Comment