0

I'm new at coding and I need help for a school project. I want to update a database using MySQL but I can't find out how to get the update working. I have googled a bit but I haven't been able to find a solution so I figured I'd ask the question on this site.

I have successfully made a connection to the database and show the contents in a data grid. The connection has a name: "conn". If anyone knows a way on how I can get the update to work I'd be happy to hear from you!

This is my XAML.CS code:

public void Click_btnBewerk(object sender, RoutedEventArgs e)
    {
        string vzitter2 = txtVZitter.Text;
        string info2 = txtInfo.Text;
        string zetels2 = txtZetels.Text;
        string stroming2 = txtStroming.Text;
        string partij = cmPartijen.Text;
        conn.Updateinfo();
    }     

This is my DBconn code:

public DataView Updateinfo()
    {
        conn.Open();
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "UPDATE partijen SET fvzitter='vzitter2', info='info2', zetels='zetels2', stroming='stroming2' WHERE partij='partij'";
        MySqlDataReader reader = command.ExecuteReader();
        DataTable dtData = new DataTable();
        dtData.Load(reader);
        conn.Close();
        return dtData.DefaultView;
    }   
Vasilisa
  • 4,604
  • 3
  • 20
  • 25
Mike VH
  • 3
  • 3
  • 1
    This has nothing to do with WPF, have a look at: https://stackoverflow.com/questions/20492019/update-statement-in-mysql-using-c-sharp – peeyush singh Feb 28 '19 at 09:16
  • Just use `command.ExecuteNonQuery();` instead of `MySqlDataReader reader = command.ExecuteReader();` – er-sho Feb 28 '19 at 09:21
  • `public void Click_btnBewerk` - I think it's good practice to keep your code fully in English, not mixing in native words. That's just a general suggestion, take it or leave it. – fstam Feb 28 '19 at 09:56

1 Answers1

0

you are doing a Reading action on the db instead an update. Just replace your code with this

public void Updateinfo()
    {
        conn.Open();
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "UPDATE partijen SET fvzitter='vzitter2', info='info2', zetels='zetels2', stroming='stroming2' WHERE partij='partij'";
        command.ExecuteNonQuery();
        conn.Close();
   }   

if you want pass the variables to the updateinfo method just do it

private void Updateinfo(string fvzitter, string info, string zetels, string stroming, string partij)
{
   string query = "UPDATE partijen SET fvzitter=@fvzitter, info=@info, zetels=@zetels, stroming=@stroming WHERE partij=@partij"
   conn.Open();
   MySqlCommand command = conn.CreateCommand();
   command.CommandText = query;
   command.Parameters.AddWithValue("@fvzitter", fvzitter);  
   command.Parameters.AddWithValue("@info", info);
   command.Parameters.AddWithValue("@zetels", zetels);
   command.Parameters.AddWithValue("@stroming", stroming);
   command.Parameters.AddWithValue("@partij", partij);

   command.ExecuteNonQuery();
   conn.Close();
}