vendredi 27 avril 2018

Multiple conditions and MySQLConnections issue in C#

I have a button click event that checks conditions within multiple input elements on a form. Textboxes, radio, combo buttons, etc.

In addition, the method check whether a text entry for username is unique or already existent in the database. If the user meets all conditions, than with the else statement is activated and the entries are stored in the database.

This is the method below:

    private void button2_Click_2(object sender, EventArgs e)
    {

        int Vid_Smetka;

        if (radioNewAdmin.Checked)
        {
            Vid_Smetka = 1;
        }
        else
        {
            Vid_Smetka = 0;
        }


        if (txtNewUser.Text == String.Empty)
        {
            MessageBox.Show("Корисничкото име е празно. Ве молиме пополнете го полето.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else if (txtNewFullname.Text == String.Empty)
        {
            MessageBox.Show("Ве молиме пополнете го полето за целосно име и презиме.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else if (txtNewEmail.Text == String.Empty || !txtNewEmail.Text.Contains('@'))
        {
            MessageBox.Show("Ве молиме пополнете го полето за email. Користете валидна e-mail адреса.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else if (txtNewPassword.Text.Length < 6)
        {
            MessageBox.Show("Ве молиме внесете најмалку 6 карактери во лозинката.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        else if (!txtNewPassword.Text.Any(x => char.IsUpper(x)))
        {
            MessageBox.Show("Ве молиме внесете барем една голема буква во лозинката.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else if (!txtNewPassword.Text.Any(x => char.IsLower(x)))
        {
            MessageBox.Show("Ве молиме внесете барем една мала буква во лозинката.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else if (txtNewPassword.Text != txtNewRepeatPassword.Text)
        {
            MessageBox.Show("Лозинките не се совпаѓаат. Ве молиме обидете се повторно.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        else if (!radioNewAdmin.Checked && !radioNewUser.Checked)
        {
            MessageBox.Show("Ве молиме селектирајте тип на сметка.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        else if (string.IsNullOrEmpty(comboNewFunction.Text))
        {
            MessageBox.Show("Ве молиме функција на корисникот на сметката во училиштето.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
        else if (txtNewUser.Text != String.Empty)
        {

            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["#.Properties.Settings.ConnectionString"].ConnectionString);

            conn.Open(); //Try to open the connection to the MySql database

            //Check if there is a row with credentials entered in the form
            MySqlDataAdapter sda = new MySqlDataAdapter("SELECT Count(*) FROM #.# WHERE Korisnik = '" + txtNewUser.Text + "'", conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            //Initiate a new connection string

            if (dt.Rows[0][0].ToString() != "0")
            {
                MessageBox.Show("Корисничкото име е зафатено. Ве молиме обидете внесете уникатно.", "Потсетник", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            conn.Close();
        }
        else
        {

            var sqlCommand = "INSERT INTO #.#" +
            "(Korisnik, Lozinka, Vid_Smetka, email, user_function, full_name) " +
            "VALUES ('" + this.txtNewUser.Text + "', '" + this.txtNewPassword.Text + "', '" + Vid_Smetka + "', " +
            "'" + this.txtNewEmail.Text + "', '" + this.comboNewFunction.Text + "', '" + this.txtNewFullname.Text + "');";

            //Initiate a new connection string

            try
            {
                using (MySqlConnection con = new MySqlConnection(conString))
                {
                    MySqlCommand cmdDatabase = new MySqlCommand(sqlCommand, con);
                    con.Open();
                    MySqlDataReader myReader;
                    myReader = cmdDatabase.ExecuteReader();
                    dataGridView1.DataSource = loadData().Tables[0];

                    MessageBox.Show("Зачувано.", "Известување", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    panelNew.Visible = false;

                }
            }
            catch (Exception ex)

            {
                MessageBox.Show(ex.Message);
            }


        } 
    }

This is obviously quite simple. But, for a certain reason the else statement is not executing when all other conditions are met.

I am making a mistake with the MySQLConnection? Any hints where I might be getting it wrong?

Aucun commentaire:

Enregistrer un commentaire