Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
this is my code to polulate a combobox with data from the database.

C#
Main_Load(object sender, EventArgs e)
        {
            bindComboBox();
        }

        public void bindComboBox()
        {
            DataTable table = new DataTable();
            conn.ConnectionString = connString;
            conn.Open();

            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM KombiStateTbl", conn);
            da.Fill(table);

            kombiComboBox.DataSource = new BindingSource(table, null);
            kombiComboBox.DisplayMember = "NumberPlate";
            kombiComboBox.ValueMember = "NumberPlate";

            conn.Close();
        }


from another form I can add another record to the database. my question is: how to update the combobox so it shows that record right after it's added to the db?


this is the code to add a record:

C#
private void okButton_Click(object sender, EventArgs e)
        {
            int intInsert = 0;
            String connString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf;Integrated Security=True;User Instance=True");
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO KombiStateTbl (NumberPlate, LicenseExpiryDate, ControlDate, "
            + "Documentos, FaróisLanternas, EstepeMacacoChaveDeRoda, Triângulo, Extintor, Acendedor, "
            + "RodasCalota, Retrovisores, Párabrisa, BancosTapetes, Pneus, ParachoqueDianteiro, "
            + "ParachoqueTraseiro, PortaDianteiraDireitaM, PortaDianteiraEsquerdaP, PortaLateral, "
            + "LateralEsquerda, LateralEsquerdaTraseira, LateralDireitaTraseira, ParalamaTraseiroDireito, "
            + "ParalamaTraseiroEsquerdo, Teto, PortaDoPortaMalas, PortaTraseiraDoMotor, Traseira, "
            + "Dianteira, FaróisDianteiros, FaroisTraseiros, CurrentPic, CurrentPicPath) VALUES "
            + "(@Param1,@Param2,@Param3, 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', "
            + "'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', "
            + "'OK', 'OK', 'OK', 'OK', NULL, NULL)";

            cmd.Parameters.Add("@Param1", SqlDbType.Char).Value = noPlateTextBox.Text;
            cmd.Parameters.Add("@Param2", SqlDbType.Char).Value = licenseDateTimePicker.Text;
            cmd.Parameters.Add("@Param3", SqlDbType.Char).Value = controlDateTimePicker.Text;
            
            cmd.Connection = conn;

            conn.Open();
            intInsert = cmd.ExecuteNonQuery();
            conn.Close();

            if (intInsert != 0)
            {
                MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                noPlateTextBox.Text = "";
                okButton.Enabled = false;                
               
            }
            else
            {
                MessageBox.Show("The data insertion is failed", "Error", MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Error);
            }            
        }

Please help.
Posted

You need to do a reload to recollect the updated content and then update the datasource of the combo box after you added that content.

this in your if statement should fix that:

C#
if (intInsert != 0)
{
    MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK,
        MessageBoxIcon.Information);
    noPlateTextBox.Text = "";
    okButton.Enabled = false;
    
    bindComboBox();        //<<- Recollect content for combobox after update.

}
 
Share this answer
 
Comments
tyska 14-Dec-12 12:36pm    
but the bindComboBox() method is in the Main form and the if statement is in another form. I've been trying to call Main form on that other form where insert occurs but with no success :(
Jibesh 14-Dec-12 13:20pm    
I wonder why are you calling MainForm. You should call mainForm.BindCombBox instead.
tyska 14-Dec-12 13:25pm    
I'm not experienced so can you please explain how can i call mainForm? i'm using VS2008.
sorry, my fault on the only calling the bind line above.

Just add the call to your if condition inside your ok button click after ok_Button.Enabled = false;

So your okButton_Click would look like this:

private void okButton_Click(object sender, EventArgs e)
        {
            int intInsert = 0;
            String connString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf;Integrated Security=True;User Instance=True");
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
 
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO KombiStateTbl (NumberPlate, LicenseExpiryDate, ControlDate, "
            + "Documentos, FaróisLanternas, EstepeMacacoChaveDeRoda, Triângulo, Extintor, Acendedor, "
            + "RodasCalota, Retrovisores, Párabrisa, BancosTapetes, Pneus, ParachoqueDianteiro, "
            + "ParachoqueTraseiro, PortaDianteiraDireitaM, PortaDianteiraEsquerdaP, PortaLateral, "
            + "LateralEsquerda, LateralEsquerdaTraseira, LateralDireitaTraseira, ParalamaTraseiroDireito, "
            + "ParalamaTraseiroEsquerdo, Teto, PortaDoPortaMalas, PortaTraseiraDoMotor, Traseira, "
            + "Dianteira, FaróisDianteiros, FaroisTraseiros, CurrentPic, CurrentPicPath) VALUES "
            + "(@Param1,@Param2,@Param3, 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', "
            + "'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', "
            + "'OK', 'OK', 'OK', 'OK', NULL, NULL)";
 
            cmd.Parameters.Add("@Param1", SqlDbType.Char).Value = noPlateTextBox.Text;
            cmd.Parameters.Add("@Param2", SqlDbType.Char).Value = licenseDateTimePicker.Text;
            cmd.Parameters.Add("@Param3", SqlDbType.Char).Value = controlDateTimePicker.Text;
            
            cmd.Connection = conn;
 
            conn.Open();
            intInsert = cmd.ExecuteNonQuery();
            conn.Close();
 
            if (intInsert != 0)
            {
                MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                noPlateTextBox.Text = "";
                okButton.Enabled = false;  
              
                Main_Load.bindComboBox();    //here is the call needed   <<<<br mode="hold" />             
            }
            else
            {
                MessageBox.Show("The data insertion is failed", "Error", MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Error);
            }            
        }
 
Share this answer
 
Comments
tyska 20-Dec-12 8:50am    
sorry but it tells me that main_Load doesn't exist in the current context.what is it that i'm missing?
i found in a book how to call the opened form: create a method in the main form getMainForm()
{
return this;
}
and then call it in the form u need

Main m = new Main();
m.getMainForm();

but it would just create another new instance of the main form.
how to solve it?
Ok, i got it. i found it somewhere on the internet. it did the job!


C#
Main m = (Main)Application.OpenForms["Main"];
                m.bindComboBox();


wolfcoder75, thank you for yr input.
 
Share this answer
 
Comments
M.Edmison 20-Dec-12 13:22pm    
Good to hear,

*Note - Voting on the answers that help the input from others improves statuses.

Thanks
M.Edmison 20-Dec-12 14:44pm    
Also, might want to mark this as solved as well.

Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900