Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi!
this is my code to polulate a combobox with data from the database.
 
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:
 
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 14-Dec-12 6:21am
tyska377
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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.

}
  Permalink  
Comments
tyska at 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 at 14-Dec-12 13:20pm
   
I wonder why are you calling MainForm. You should call mainForm.BindCombBox instead.
tyska at 14-Dec-12 13:25pm
   
I'm not experienced so can you please explain how can i call mainForm? i'm using VS2008.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
            }            
        }
  Permalink  
Comments
tyska at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Ok, i got it. i found it somewhere on the internet. it did the job!
 

Main m = (Main)Application.OpenForms["Main"];
                m.bindComboBox();
 
wolfcoder75, thank you for yr input.
  Permalink  
Comments
wolfcoder75 at 20-Dec-12 13:22pm
   
Good to hear,
 
*Note - Voting on the answers that help the input from others improves statuses.
 
Thanks
wolfcoder75 at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100