Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a problem, which I can not understand. I want to send notification to client as soon as data is insert in [dbo].[Notifications] table. for this I am using sqldependency for rise onchange event and to sent information client with SignalR. Problem is that when i make a first Change the Event fires, But if i made a second change the Event does not Fire any more

C#
 public class NotoficationRepository : BaseClass
    {
        public int IsNotification(int UserID)
        {
            try
            {
                private string sqlString = string.Format("SELECT COUNT_BIG(*) AS [COUNT]
                 FROM [dbo].[Notifications] 
                 WHERE UserID = '{0}' AND 
                 IsNotificationShow = '{1}'", UserID, false);

                var count = 0;
                SqlDependency.Stop(ConnectionSring);
                SqlDependency.Start(ConnectionSring);
                using (var connection = new SqlConnection(ConnectionSring))
                {
                    connection.Open();
                    using (command = new SqlCommand(sqlString, connection))
                    {
                        command.Notification = null;

                        SqlDependency dependency = new SqlDependency(command);
                        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                        if (connection.State == ConnectionState.Closed)
                            connection.Open();
                        count = int.Parse(command.ExecuteScalar().ToString());
                    }
                }    

                return count;    
            }
            catch
            {
                throw;
            }
            finally
            {
                base.Dispose();
            }
        }
}


OnChangeEvent
C#
void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Info == SqlNotificationInfo.Insert)
        {
            MessagesHub.SendMessages();
        }         
    }


BaseClass
C#
public class BaseClass : IDisposable
    {
        private bool disposed = false;

        protected NotificationEntities DB;

        protected string ConnectionSring;
        public BaseClass()
        {
            DB = new NotificationEntities();
            ConnectionSring = DB.Database.Connection.ConnectionString;
        }

        ~BaseClass()
        {
            Dispose(false);
        }

        public void Dispose()
        {            
            Dispose(true);
            GC.SuppressFinalize(this);            
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
            {
                if (DB != null)
                {
                    DB.Dispose();
                    DB = null;
                }
            }

            disposed = true;
        }
}


Can you help me?? what is the problem?
Posted
Comments
Sinisa Hajnal 13-Mar-15 7:43am    
Did you check that your e.Info is really Insert?
Sider89 13-Mar-15 8:10am    
Yes, if I insert row in the table, e.info has 'insert' flag
Sider89 14-Mar-15 6:29am    
any ideas?

1 solution

recall IsNotification() method in dependency_OnChange.
good luck
 
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