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:
ASPX.cs
C#
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 SqlConnection= "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();
}
}
}
}


ASPX
ASP.NET
<script>
        $(function () {
            var notify = $.connection.notificationsHub;
            notify.client.displayNotification = function (msg) {
                $("#GridView1").html(msg);
            };

            $.connection.hub.start();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" />
                    <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                    <asp:BoundField DataField="Mail" HeaderText="Mail" SortExpression="Mail" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT [Message], [Id], [Mail] FROM [DummyData]"></asp:SqlDataSource>
        </div>

NotificationHub.cs
C#
    public class NotificationsHub : Hub
    {
        public void NotifyAllClients(string msg)
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
            context.Clients.All.displayNotification(msg);
        }
    }
}
Posted

1 solution

That's because you replace the content of your #GridView1 element instead of appending to it...


You can see this chat example: http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc. It seems like it's dealing with the same issue.

 
Share this answer
 
Comments
Member 11893772 29-Nov-15 7:31am    
please i didn't get it could u show me wat code i have missed
Shmuel Zang 29-Nov-15 8:22am    
In your code you replace all the content of your element. You should keep its content and, just append to it. And, since you want to manage your grid in the client side, instead of using an ASP.NET WebForms control that is managed by the server, it's can be more convenient to use simple HTML tags that can be managed in the client side. Go over the given chat example. You can see how they append an li element (for each message) to an ul element. In your case, if you want to show a grid, you can use a table (and add tr elements) instead of a list.

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