Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a listbox conected to a sql table, the table previews whats in the table,so when i add to the table it shows in the listbox, fair enough it works but only if i reload the form it updates , heres my code
C#
DataSet st = new DataSet();
            string strConnectionString = "Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True";

            SqlConnection objconnection = new SqlConnection(strConnectionString);
            using (SqlCommand cmd = new SqlCommand("SELECT  [name] FROM [staff1]",
            objconnection))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(st);
                }
            }

            var empList = st.Tables[0].AsEnumerable().Select(dataRow =>

            dataRow.Field<string>("name")).ToList();
            listBox1.DataSource = empList;

            listBox1.SelectedIndex = 0;
            listBox1.Refresh();

tried listBox1.Refresh(); almost everywhere , didnt help, 
also tried  public void RefreshItem2(int index)
            {
                RefreshItem(index);
            }

any help would be apreciated :)
Posted
Updated 28-Aug-15 0:18am
v3

1 solution

you need to subscribe update notifications from database. search for SqlDependency and how you can use it to track database changes.
Another option is using timer and periodically check whether new records available or not. On timer elapsed event you can bind data to your control.
 
Share this answer
 
Comments
jamesmc1535 31-Aug-15 5:08am    
how would i do it using a timer? added a timer. on events i added >
listbox1.indexchanged >
with the listbox1.refresh();
?

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