Question

I have a datagridview bound to a datasouce, bound to a datatable. I want to create a button that when I click it, the database will be updated with the new parameters in the datagridview. I've read some stuff about TableAdapter but I cant really find some good examples and explanations.

So if someone can give some information about tableadapter, it will help me a lot. Also, if you think that you have a better solution for me regarding update the database, i'll be glad to know about that.

EDIT: ok so i'm trying to use the mysqlcommandbuilder. My code now looks like this:

                MySqlDataAdapter da = new MySqlDataAdapter();
            da.SelectCommand = new MySqlCommand("SELECT * from setups", sql_Class.myConnection);
            MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
            cb.GetUpdateCommand();

            da.Update(dt);

so now my error is in the da.Update(dt) and it says: "input string was not in the correct format". I've created a loop that runs on all the parameters in the sql query and all the parameters values are null.

            for (int i = 0; i <= cb.GetUpdateCommand().Parameters.Count - 1; i++)
            {
                Console.WriteLine(cb.GetUpdateCommand().Parameters[i].ParameterName + " " + cb.GetUpdateCommand().Parameters[i].Value);
            }

I have values in the datatable, i've double checked it, but someone the parameters are null. Any ideas why?

Was it helpful?

Solution

Managed to update my database in some "old fashion" method.

I'm just running on every single row in the datagridview and updates it using executenonquery. I get all the parameters manually.

Example: int setup_id = Convert.ToInt16(stam.Cells["setup_id"].Value); string setup_name = stam.Cells["setup_name"].Value.ToString(); string mySqlQuery = "UPDATE setups SET setup_name='"+setup_name+"' WHERE setup_id="+setup_id;

MySqlCommand cm = new MySqlCommand(mySqlQuery, sql_Class.myConnection); cm.ExecuteNonQuery();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top