Click here to Skip to main content
15,888,218 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all ,

I am trying to update my list view when anything changes in the database in real time.Which means i need to get notified automatically on any change in my database and then the listview data changes on the page without refreshing.So i enabled broker for the northwind database and wrote this code :

C#
protected void Page_Load(object sender, EventArgs e)
 {
GetNames();

}


C#
private void GetNames()
       {
           lbNames.Items.Clear();
           SqlDependency.Stop(connect);
           SqlDependency.Start(connect);
           using (SqlConnection cn = new SqlConnection(connect))
           {
               using (SqlCommand cmd = cn.CreateCommand())
               {
                   cmd.CommandType = CommandType.Text;
                   cmd.CommandText = "SELECT FirstName, LastName FROM dbo.[Employees]";

                   cmd.Notification = null;
                   dep.OnChange += new OnChangeEventHandler(dep_onchange);

                   cn.Open();

                   using (SqlDataReader dr = cmd.ExecuteReader())
                   {
                       while (dr.Read())
                       {
                           lbNames.Items.Add(dr.GetString(0) + " " + dr.GetString(1));
                       }
                   }
               }
           }
       }


C#
public void dep_onchange(object sender, SqlNotificationEventArgs e)
{
    GetNames();
    SqlDependency dep = sender as SqlDependency;
    dep.OnChange -= new OnChangeEventHandler(dep_onchange);
}


but nothing is changed. So any ideas ?
Posted

1 solution

try to Examine this Article

~Click Here~



Hope it helps
 
Share this answer
 

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