Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is html

XML
<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script>
    <script src="Scripts/jquery-jtemplates.js" type="text/javascript"></script>
    <!--Reference the SignalR library. -->
    <script src="/Scripts/jquery.signalR-2.2.0.js"></script>
    <script src="/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="/signalr/hubs"></script>

     <script type="text/javascript">

         $(function () {
             var notify = $.connection.notificationsHub;

             notify.client.displayNotification = function (msg) {
                 alert(msg);

             };

             $.connection.hub.start();
         });
    </script>



and the Connection String :

XML
<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=TestDB;Integrated Security=True" providerName="System.Data.EntityClient" />
    </connectionStrings>


and this is the page

  public partial class Test : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           SendNotifications();
        }

        List<Model.Suggestion> result;
        public List<Model.Suggestion> SendNotifications()
        {

            string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;


            using (SqlConnection connection = new SqlConnection(conStr))
            {
                string query = @"select top 5 [sgtID] , [Name] , [Phone] , [Gender] , [Age] ,[AnswerPerc] , [AnswerRank] , [date] , [Notes] from [dbo].[Suggestion] order by date desc";

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Notification = null;
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        //result = reader[0].ToString();
                    }
                }
            }
            NotificationsHub nHub = new NotificationsHub();
            //nHub.NotifyAllClients(message);
            nHub.NotfiyAllClients(result);
            return result;
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                SendNotifications();
            }
        }
    }


and this is the hub Class

namespace TestSignalR.Hubs
{
    public class NotificationsHub : Hub
    {
        private static string conString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();


        public void NotfiyAllClients(List<Model.Suggestion> sgt)
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
            context.Clients.All.displayNotification(sgt);
        }
    }
}


Here is the global class:

 string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        protected void Application_Start(object sender, EventArgs e)
        {
            SqlDependency.Start(connString);
        }
Posted
Updated 28-Apr-15 21:37pm
v6
Comments
virusstorm 27-Apr-15 12:48pm    
Can you clarify "doesn't work"? Do you get an error?
Mohamed Esmail 28-Apr-15 8:35am    
Notification after any change in DataBase doesn't work
virusstorm 28-Apr-15 13:11pm    
Do you mean this line isn't working?
nHub.NotfiyAllClients(result);
Mohamed Esmail 29-Apr-15 3:42am    
I mean that when i change a record in database these line nHub.NotfiyAllClients(result);
doesn't work as you said.
and the event (dependency_OnChange) doesn't loaded
ZurdoDev 27-Apr-15 14:08pm    
Need more info.

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