Refreshing ComboBox in a winform in C#.net after inserting new datarecords in table

B

b_nursel

my combobox(cbo_Bezeichnung) is displaying data from
tbl_NCC_Gegenstaende. Now if i insert a new record in this table and i
want to refresh my combobox with this new added record is displaying
with the other records, too. How can i do it. It is very urgent.

My source code as follows:

SqlCeConnection myConnection = new
SqlCeConnection(myConnectionString);
string select="SELECT Bezeichnung_ID, Bezeichnung FROM
tbl_NCC_Gegenstaende ORDER BY Bezeichnung_ID";
SqlCeDataAdapter myDataAdapter = new SqlCeDataAdapter();
myDataAdapter.SelectCommand = new SqlCeCommand(select,myConnection);
SqlCeCommandBuilder myCommandBuilder = new
SqlCeCommandBuilder(myDataAdapter);

myConnection.Open();
DataSet ds=new DataSet();


myDataAdapter.Fill(ds,"tbl_NCC_Gegenstaende");


if(ds.Tables["tbl_NCC_Gegenstaende"].Rows.Count==0)
{
MessageBox.Show("Es sind noch keine Gegenstände eingetragen");
}
else
{
string[] NewRow = {"-1", "Neue Bezeichnung"};
ds.Tables["tbl_NCC_Gegenstaende"].Rows.Add (NewRow);

cbo_Bezeichnung.Items.Clear(); // cbo_Bezeichnung is my ComboBox
cbo_Bezeichnung.DataSource =ds.Tables["tbl_NCC_Gegenstaende"];
cbo_Bezeichnung.DisplayMember = "Bezeichnung";
cbo_Bezeichnung.ValueMember = "Bezeichnung_ID";
cbo_Bezeichnung.SelectedIndex = -1;


}
myConnection.Close();

SqlCeCommand cmd2= myConnection.CreateCommand();
myConnection.Open();
cmd2.CommandText = "INSERT INTO tbl_NCC_Gegenstaende (Bezeichnung_ID,
Bezeichnung) VALUES (5, 'Tisch')";
cmd2.ExecuteNonQuery();
myConnection.Close();

// Here i want to refresh my combobox with this inserted record is
also displaying in the combobox.

Please Help!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top