Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server WinForm , +
i have a form with a comboBox, a textBox, and a button. the comboBox shows items from an sql database using dataBindingSources. after filling the textBox and clicking the button, a new item is added to this database, but i can't see this new item in the comboBox until i close and re-open the windowsForm. how to refresh the comboBox without close/re-open???
 
here's the code:
 
private void button2_Click(object sender, EventArgs e)
        {
            {
 
                SqlConnection myConnection = new SqlConnection("****");
                myConnection.Open();
                
                try
                {
                    SqlCommand myCommand = myConnection.CreateCommand();
                    myCommand.CommandText = "INSERT INTO Items (NameItem) " +
                "Values ('" + textBox2.Text + "')";
 
                    myCommand.ExecuteNonQuery();
                    MessageBox.Show("Item Added", "OK");
       
                }
                catch (SqlException exp)
                {
                    MessageBox.Show(exp.Message);
                }
 
                finally
                {
                    
                    myConnection.Close();
                    textBox2.Clear();
                    
                    
                }
              
            }
Posted 6-May-13 10:04am
Nnorss521
Comments
richcb at 6-May-13 15:08pm
   
I would recommend re-binding the datasource in your "finally". That will have the combobox repopulate with the new data.
Naourass Derouichi at 6-May-13 15:28pm
   
How to rebind?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Rebind the combo box in your button click event, after adding the item.
  Permalink  
Comments
Naourass Derouichi at 6-May-13 15:28pm
   
how to rebind?
Teenustar at 6-May-13 15:35pm
   
How did you bind the combo box in the form load? Similarly.
Naourass Derouichi at 10-May-13 13:49pm
   
i binded my combobox by clicking on the combobox arrow and checking use data elementsthen selecting the db and the table.That was in the form design!
Teenustar at 10-May-13 14:45pm
   
Yes. Thats one way of doing it. But there are other ways. Please refer http://msdn.microsoft.com/en-us/library/w67sdsex.aspx
Naourass Derouichi at 11-May-13 12:58pm
   
ok thannks

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

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 95
4 Maciej Los 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 6 May 2013
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