0

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!

Ezio Farenze
  • 89
  • 1
  • 1
  • 7
  • 1
    Note*: `HasRows` will only check if table contains a row or rows which is self explanatory so in your code the if-statement will be always true unless table is empty. – P. Pat Mar 02 '17 at 08:58
  • Possible duplicate of [Check if a record exists in the database](http://stackoverflow.com/questions/21302244/check-if-a-record-exists-in-the-database) – Mong Zhu Mar 02 '17 at 09:21
  • @MongZhu He's not asking how to check it, he's asking how to fix both messages showing (user exists but it will still try to insert). – EpicKip Mar 02 '17 at 09:32

1 Answers1

0

Make your userCheck method return a bool like this UserExists method.
It works as follows:
Before storing the user the if checks if the user exists.
In the UserExists method you display an error if the user exists and return true.
Then if it returns true the StoreUser method will return before it will store the user.
And if it returns false (User does not exist) the StoreUser method continues as normal.

    public static void StoreUser()
    {
        if ( UserExists( "username" ) )
        {
            //Will stop here if user exists
            return;
        }
        //Storing user code
    }
    public static bool UserExists(string userName)
    {
        if (userName == "EXISTS")
        {
            //Show error
            return true;
        }
        return false;
    }
EpicKip
  • 4,015
  • 1
  • 20
  • 37