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;
    }   
3
  • 1
    This has nothing to do with WPF, have a look at: stackoverflow.com/questions/20492019/… Commented Feb 28, 2019 at 9:16
  • Just use command.ExecuteNonQuery(); instead of MySqlDataReader reader = command.ExecuteReader(); Commented Feb 28, 2019 at 9: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. Commented Feb 28, 2019 at 9:56

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This fixed my problem!!
glad to help you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.