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!

3
  • 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. Commented Mar 2, 2017 at 8:58
  • Possible duplicate of Check if a record exists in the database Commented Mar 2, 2017 at 9: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). Commented Mar 2, 2017 at 9:32

1 Answer 1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.