Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void pictureBox1_Click(object sender, EventArgs e)
        {
            System.IO.Stream str = Properties.Resources.commander_5;
            System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
            snd.Play();

            label1.Text = "S1";



            
   string constring = "datasource=123.456.678;port=3306;username=1234;password=1234";
            string query = "update mysqlcshap.Fahrzeug set (Status) TEXT('1');";
            MySqlConnection Dateneingabe = new MySqlConnection(constring);
            MySqlCommand cmd = new MySqlCommand(query, Dateneingabe);
            MySqlDataReader myReader;


            try
            {
                Dateneingabe.Open();
                myReader = cmd.ExecuteReader();
           
                while (myReader.Read()) { }





            }




            catch (Exception ex)
            {  }


What I have tried:

How can I update the Text on MYSQL on Buttonclick?
Posted
Updated 11-Jul-18 6:03am

1 solution

The query should look more like this
"update mysqlcshap.Fahrzeug set Status='1';"
However, this will update all the records in the database which is probably not what you want. You probably want to update a specific record and for that you need a WHERE clause
"update mysqlcshap.Fahrzeug set Status='1' WHERE Id=1234;"
This assumes that you have a primary key value in your table and it's name is 'Id'. Of course you need to know what the Id is in order to do this.
Sometimes you can create fields that are reserved words. I don't remember if 'Status' is one of them, but just case wrap the field names in [].
You're using ExecuteReader. This query doesn't return anything so there's nothing to read. Use ExecuteNonQuery to run a command that doesn't read.
Your connection string needs fixing (see this. I don't see a 'Database' argument, perhaps it's the mysqlcshap?
I usually wrap my connection in a using statement. Put all that together and this is what I came up with
C#
private void pictureBox1_Click(object sender, EventArgs e)
{
    System.IO.Stream str = Properties.Resources.commander_5;
    System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
    snd.Play();
    label1.Text = "S1";
    
    string constring = "datasource=123.456.678;Database=mysqlcshap;port=3306;User Id=1234;Password=1234";
    string query = "update Fahrzeug set [Status]='1' WHERE Id=1234;";
    using (MySqlConnection Dateneingabe = new MySqlConnection(constring))
    {
        MySqlCommand cmd = new MySqlCommand(query, Dateneingabe);
        try
        {
            Dateneingabe.Open();
            cmd.ExecuteNonQuery();
            Dateneingabe.Close();
        }
        catch (Exception ex)
        { 
            // DO SOMETHING WITH THE EX HERE!! 
        }
    }
}
HTH, Mike
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900