Daniel Scuba
Daniel Scuba

Reputation: 57

C# and MySQL error using Paramters.AddWithValue returns parameter denotations

Normally I don't ask questions on here because there is already so many good answers that answer my question, however this time I have been searching for a little over 2 hours with no luck, so here we go (BTW glad to be wrong on that, feel free to point me in the right direction.)

I am trying to do a parameterized input into my database, but I keep getting the placeholder parameters instead of the dynamic values I assign.

        string name = //definitely a string value.
        string dc = //definitely a string value.
        string password = //definitely a string value (hashed, no plaintext.)
        string passwordDate = //definitely a date value.
        int passwordAge = //definitely a int value.
        string role = //definitely a string value.
        string shift = //definitely a string value.
        string modBy = //definitely a string value.
        string newEmpQuery = "INSERT INTO /*table address*/ (`/*Field1*/`,`/*Field2*/`,`/*Field3*/`,`/*Field4*/`,`/*Field5*/`,`/*Field6*/`,`/*Field7*/`,`/*Field8*/`) VALUES ('@name1','@dc1','@pass1','@pwda1','@pwage1','@role1','@shift1','@mod1') ON DUPLICATE KEY UPDATE `/*Field2*/`='@dc2', `/*Field3*/`='@pass2', `/*Field4*/`='@pwda2', `/*Field5*/`='@pwage2', `/*Field6*/`='@role2', `/*Field7*/`='@shift2', `/*Field8*/`='@mod2';";
        string modEmpQuery = "INSERT INTO /*table address*/ (`/*Field1*/`,`/*Field2*/`,`/*Field6*/`,`/*Field7*/`,`/*Field8*/`) VALUES ('@name','@dc1','@role1','@shift1','@mod1') ON DUPLICATE KEY UPDATE `/*Field2*/`='@dc2', `/*Field6*/`='@role2', `/*Field7*/`='@shift2', `/*Field8*/`='@mod2';";
        if (passwordResetButton.Visible == true && newRadioButton.Checked == false)
        {
            //Build Query
            Program.conn.Open();
            using (MySqlCommand data = new MySqlCommand(modEmpQuery, Program.conn))
            {
                data.Prepare();
                data.Parameters.AddWithValue("@dc1", dc);
                data.Parameters.AddWithValue("@dc2", dc);
                data.Parameters.AddWithValue("@name", name);
                data.Parameters.AddWithValue("@role1", role);
                data.Parameters.AddWithValue("@role2", role);
                data.Parameters.AddWithValue("@shift1", shift);
                data.Parameters.AddWithValue("@shift2", shift);
                data.Parameters.AddWithValue("@mod1", modBy);
                data.Parameters.AddWithValue("@mod2", modBy);

                //attempt Data Submission
                data.ExecuteReader();

                MessageBox.Show("Employee " + name + " successfully modified!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            Program.conn.Close();
            ClearFields();
        }

This is just a snippet (sanitized to protect innocent databases everywhere [ok maybe just mine]), but part of the reason this looks so messy is I have been trying to exhaust every possibility. I changed my parameter denoters from '@' to '?' as one post I found tried. I obviously tried adding variance to my repeating parameters as well. It has been prepared() and the connection is open, but upon execution, I always get "@dc" instead of the value I assign afterwards.

Upvotes: 2

Views: 90

Answers (1)

Oliver Sellar
Oliver Sellar

Reputation: 46

I don't think you need the '' around the parameters maybe try removing the '' around the parameters in the INSERT statments

Upvotes: 3

Related Questions