i'm trying to create a error handler which you can't save values if values is already existing in the database, But yes, the error message is showing and the dialog result is showing too at the same time, i want to show the error if the values are existing & if the values are not existing, the dialog result will show.
here's my code:
usercheck();
DialogResult dr = MetroMessageBox.Show(this, "Are you sure you want to Save without your desire Sales Count or Sales Amount?", "Wait!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (txtCount.Text == "0" || txtAmount.Text == "0")
{
if (dr == DialogResult.Yes)
{
Data.con.Open();
string SaveStr = "Insert into dbinfo.tbluser (UserID, UserName, UserPassword, UserLevel, TargetCount, TargetAmount) Values (@UserID, @UserName, @UserPassword, @UserLevel, @TargetCount, @TargetAmount)";
MySqlCommand SaveCmd = new MySqlCommand(SaveStr, Data.con);
SaveCmd.Parameters.AddWithValue("@UserID", txtID.Text);
SaveCmd.Parameters.AddWithValue("@UserName", txtName.Text);
SaveCmd.Parameters.AddWithValue("@UserPassword", txtPassword.Text);
SaveCmd.Parameters.AddWithValue("@UserLevel", cbLevel.Text);
SaveCmd.Parameters.AddWithValue("@TargetCount", txtCount.Text);
SaveCmd.Parameters.AddWithValue("@TargetAmount", txtAmount.Text);
SaveCmd.ExecuteNonQuery();
Data.con.Close();
LoadData();
MetroMessageBox.Show(this, "User Entry Saved!", "Saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
clear();
}
else if (dr == DialogResult.No)
{
clear();
}
}
and here's my code from the method usercheck()
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbluser where UserName=@UserName";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand Check = new MySqlCommand(Query, con);
Check.Parameters.AddWithValue("@UserName", this.txtName.Text);
con.Open();
MySqlDataReader dr = Check.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows)
{
MetroMessageBox.Show(this, "The User Name " + dr[1].ToString() + " Already exist!","Existing User", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtName.Clear();
break;
}
else
{
txtCheck.Visible = false;
}
}
Thank you!