简体   繁体   中英

Refreshing the combobox after inserting data in mysql c#

Hello so i want to refresh my combobox after i add or delete data from it now if i add data it doesnt get refreshed i have to rerun the program to see the changes but i want to get it refresh in the time i add the data..

the code when i add data:

private void button5_Click(object sender, EventArgs e)
    {
        MySqlConnection dataConnection = new MySqlConnection();
        dataConnection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
        dataConnection.Open();
        MySqlTransaction transakcija = dataConnection.BeginTransaction();
        MySqlCommand dataCommand = new MySqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.Transaction = transakcija;
        try
        {
            dataCommand.CommandText = "Insert INTO filmi.film (film) VALUES ('" + this.tB_Dodaj.Text + "')";
            dataCommand.CommandType = CommandType.Text;
            dataCommand.ExecuteNonQuery();
            transakcija.Commit();
            MessageBox.Show("You added a new movie!");
        }
        catch (Exception eks)
        {
            transakcija.Rollback();
            MessageBox.Show("Movie couldnt be added!!\n" + eks.Message);
        }
        finally
        {
            dataCommand.Connection.Close();
        }
    }

and with each insert the data gets displayed in the combobox but only when i rerun the program

this is how i fill combobox:

void Fillcombo()
    {
        string constring = "datasource=localhost;port=3306;username=root;password=";
        string Query = "SELECT * FROM filmi.film ;";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                string sName = myReader.GetString("film");
                comboBox1.Items.Add(sName);
                comboBox2.Items.Add(sName);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

i have to rerun the program to see the changes but i want to get it refresh in the time i add the data..

I suspect that you are calling the Fillcombo() method in Form_Load event handler.

if you want to update the combobox for every insert and delete operations in your table you need to call Fillcombo() immediatly after executing the command.

Try This:

int status = dataCommand.ExecuteNonQuery();
transakcija.Commit();
if(status > 0)
{
    Fillcombo();
    MessageBox.Show("You added a new movie!");
}

in your FillCombo clear the items before adding the new items to remove the duplicates.

comboBox1.Items.Clear(); //add this statetement before adding items
comboBox2.Items.Clear(); //add this statetement before adding items
while (myReader.Read())
{
    string sName = myReader.GetString("film");
    comboBox1.Items.Add(sName);
    comboBox2.Items.Add(sName);
}

ADO.NET object works in a disconnected state, so, adding a record to your datatable doesn't automatically shows up in your combo. You need to call again FillCombo, (clearing the items already in combos, going again to the database to retrieve every record again, adding them to your comboboxes) or just simply adding the film to your already filled combos as a single item

Also pay attention to Sql Injection (use always a parameterized query)

private void button5_Click(object sender, EventArgs e)
{
    string conString = "datasource=localhost;port=3306;username=root;password=";
    string cmdText = "Insert INTO filmi.film (film) VALUES (@film)";
    using(MySqlConnection dataConnection = new MySqlConnection(conString))
    using(MySqlCommand dataCommand = new MySqlCommand(cmdText, dataConnection))
    {
         try
         {
            dataConnection.Open();
            dataCommand.Parameters.AddWithValue("@film", this.tB_Dodaj.Text);

            // If ExecuteNonQuery returns a value > 0 then your record has been inserted
            // Just add the name of the film to the two combos 
            if(dataCommand.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("You added a new movie!");
                comboBox1.Items.Add(this.tB_Dodaj.Text);
                comboBox2.Items.Add(this.tB_Dodaj.Text);
            }
         }
         catch(Exception ex)
         {
            MessageBox.Show("Fail to add a new movie! " + ex.Message);
         }
    }
}

As a last note, watch carefully your fillcombo method. You don't close and dispose the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM