Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in MySqlCommand: program doesn't work right

I have this code:

        private void FormProduction_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (name != string.Empty && school != string.Empty)
            {
                if (id == -1) 
                {
                    MySqlConnection conn = new MySqlConnection(connStr);
                    MySqlCommand cmd = new MySqlCommand("INSERT Person(Name, School) VALUES (name, school)", conn);
                    conn.Open();
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@school", school);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                else
                {
                    MySqlConnection conn = new MySqlConnection(connStr);
                    MySqlCommand cmd = new MySqlCommand("UPDATE Person SET Name=name, School=school WHERE Id=id", conn);
                    conn.Open();
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@school", school);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }

                Administration.fill();
            }
        }

And my table Person doesn’t change when I execute a program (any inserts and any updates). Where’s an error?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Hope this can help you:

More information on MySQL parametrized queries (official doc.)

private void FormProduction_FormClosed(object sender, FormClosedEventArgs e)
            {
                if (name != string.Empty && school != string.Empty)
                {
                    if (id == -1) 
                    {
                        MySqlConnection conn = new MySqlConnection(connStr);
                        MySqlCommand cmd = new MySqlCommand("INSERT Person(Name, School) VALUES (@name, @school)", conn);
                        conn.Open();
                        cmd.Parameters.AddWithValue("@name", name);
                        cmd.Parameters.AddWithValue("@school", school);
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                    else
                    {
                        MySqlConnection conn = new MySqlConnection(connStr);
                        MySqlCommand cmd = new MySqlCommand("UPDATE Person SET Name=@name, School=@school WHERE Id=@id", conn);
                        conn.Open();
                        cmd.Parameters.AddWithValue("@name", name);
                        cmd.Parameters.AddWithValue("@school", school);
                        cmd.Parameters.AddWithValue("@id", id);
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
    
                    Administration.fill();
                }
            }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading