D
D
deadspace122022-04-18 18:41:12
MySQL
deadspace12, 2022-04-18 18:41:12

How to update db content from dataGridView?

The essence of my task is that by clicking on any row and when clicking, the program automatically updates the value of the datagridView cell and that this changed table is updated in the MySQL database, but nothing happens.
Here is the code of my table, where I display the table through the dataset:

private void button1_Click(object sender, EventArgs e)
        {
            string CommandText = "Select " +
                "transportation.number as 'номер перевозки', " +
                "ticket.Name as 'ФИО пассажира', " +
                "route.Route_number as 'номер маршрута', " +
                "route.destination as 'место назначения', " +
                "ticket.Place as 'Место', " +
                "ticket.Cost as 'Стоимость', " +
                "driver.Nname as 'ФИО водителя', " +
                "transportation.active as 'Активность строки' " +
            "From " +
                "auto.transportation, " +
                "auto.route, " +
                "auto.ticket, " +
                "auto.driver " +
            "Where " +
                "(transportation.ID_Route = route.ID_Route) AND " +
                "(transportation.id_Ticket = ticket.id_Ticket) AND " +
                "(transportation.id_Driver = driver.id_Driver)";
            MySqlCommand cmd = new MySqlCommand(CommandText);
            if (textBox1.Text != "")
            {
                if (comboBox1.SelectedIndex == 0)
                    CommandText = CommandText + " AND (transportation.number = '" + textBox1.Text + "')";
                if (comboBox1.SelectedIndex == 1)
                    CommandText = CommandText + " AND (route.Route_number = '" + textBox1.Text + "')";
                if (comboBox1.SelectedIndex == 2)
                    CommandText = CommandText + " AND (route.destination LIKE '" + textBox1.Text + "%') ";
                if (comboBox1.SelectedIndex == 3)
                    CommandText = CommandText + " AND (ticket.Name LIKE '" + textBox1.Text + "%') ";
                if (comboBox1.SelectedIndex == 4)
                    CommandText = CommandText + " AND (driver.Nname LIKE '" + textBox1.Text + "%') ";
            }
            adapter = new MySqlDataAdapter(CommandText, conn);
            ds = new DataSet();
            adapter.Fill(ds, "transportation");
            dataGridView1.DataSource = ds.Tables["transportation"].DefaultView;
        }

and the code of the button, where I try to simultaneously automatically update those to insert the value of the cell datagridview.Cells[7] 7 in a row and immediately update this changed table, but it only turns out to fill the table with the same value but not fill it:
private void button3_Click(object sender, EventArgs e)
        {
            Form3 f = new Form3();
            int row = dataGridView1.CurrentCell.RowIndex;
            if (f.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MySqlCommand command = new MySqlCommand("Update transportation set active = @active where [email protected]", conn);
                    command.Parameters.AddWithValue("@active", dataGridView1.Rows[row].Cells[7].Value = "X");
                    command.Parameters.AddWithValue("@id", dataGridView1.Rows[row].Cells[0].Value);
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

Help me figure out how to solve the problem, I think it's all about my active variable, but I don't know how to properly declare it so that it's possible to update the database from the datagridview.
Thanks in advance!!!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question