locked
My code not working anymore RRS feed

  • Question

  • User932259438 posted

    Hi,

    Yesterday my code was working, now I tried and not working.

    C# is ok. Problem is because not retrieve any message. I tried also alert and I got "undefined". I checked errors. All is ok.

    using DatabaseNotification;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Messages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SendNotifications();
        }
        public void SendNotifications()
        {
            string message = string.Empty;
            string conStr = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
    
            using (SqlConnection connection = new SqlConnection(conStr))
            {
                string query = "SELECT [Message] FROM [dbo].[DummyData]";
    
                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();
                        message = reader[0].ToString();
                    }
                }
            }
            NotificationsHub nHub = new NotificationsHub();
            nHub.NotifyAllClients(message);
        }
    
        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                SendNotifications();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Microsoft.AspNet.SignalR;
    
    namespace DatabaseNotification
    {
        public class NotificationsHub : Hub
        {
            public void NotifyAllClients(string msg)
            {
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
                context.Clients.All.displayNotification(msg);
            }
        }
    }
    
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Messages.aspx.cs" Inherits="Messages" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    
    
    
    <script>
    
        $(function () {
            var notify = $.connection.notificationsHub;
    
            notify.client.displayNotification = function (msg) {
                $("#newData").html(msg);
            };
    
            $.connection.hub.start();
        });
    </script>
    
    
    
    
    <script src='<%=ResolveClientUrl("~/Scripts/jquery-2.2.0.min.js") %>' type="text/javascript"></script>
    <script src='<%=ResolveClientUrl("~/Scripts/jquery.signalR-2.2.0.min.js") %>' type="text/javascript"></script>
    <script src='<%=ResolveClientUrl("signalr/hubs") %>' type="text/javascript"></script>
    
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    <div>
                <span id="newData"></span>
            </div>
        </form>
    </body>
    </html>
    

    Example from: http://ruchirac.blogspot.si/2014/10/get-database-change-notification-in.html

    This code returns in c#, but in span "NewData" not returns nothing is empty. I tried also alert but I got "Undefined".

    Can you please make test?
    Thank you.

    Tuesday, January 2, 2018 9:48 PM

Answers

  • User61956409 posted

    Hi progy85,

    <o:p></o:p><o:p>

    in span "NewData" not returns nothing is empty. I tried also alert but I got "Undefined"
    </o:p>

    The JavaScript function displayNotification will be called in your hub method NotifyAllClients, and the hub method NotifyAllClients is called via nHub.NotifyAllClients(message) in your SendNotifications method, if your client do not receive message, please try to set breakpoint in your SendNotifications method and trace the value of the variable message (message = reader[0].ToString()).<o:p></o:p>

    With Regards, <o:p></o:p>

    Fei Han<o:p></o:p>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 2:23 AM