0
votes

I want to create a grid and automatic update it, when data changes in the database. It works with a simple table control.

public partial class index : System.Web.UI.Page
{
    static string connectionString = @"Data Source=*******;initial catalog=Test;persist security info=True;  Integrated Security=SSPI;";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static IEnumerable<Person> GetData()
    {
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(@"SELECT [Id],[Name] FROM  [dbo].[Persons]", connection))
            {

                command.Notification = null;
                SqlDependency.Start(connectionString);
                SqlDependency dependency = new SqlDependency(command);
                dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);

                if (connection.State == ConnectionState.Closed)
                    connection.Open();

                using (var reader = command.ExecuteReader())
                    return reader.Cast<IDataRecord>()
                        .Select(x => new Person(x.GetInt32(0), x.GetString(1))).ToList();
            }
        }
    }

    private static void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        MyHub.Show();
    }

    public void FillGrid()
    {
        List<Person> persons = new List<Person>();
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(@"SELECT [Id],[Name] FROM  [dbo].[Persons]", connection))
            {

                if (connection.State == ConnectionState.Closed)
                    connection.Open();

                using (SqlDataReader rdr = command.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        var id = rdr.GetInt32(0);
                        var name = rdr.GetString(1);
                        persons.Add(new Person(id, name));
                    }
                }
            }
        }
        grid.DataSource = persons;
        grid.DataBind();
    }
}

public class MyHub : Hub
{
    public static void Show()
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.All.displayStatus();
    }
} 

And the apsx Page :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.6.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
 <script src="signalr/hubs"></script>
<script type="text/javascript">

    $(function () {

        // Proxy created on the fly
        var job = $.connection.myHub;

        // Declare a function on the job hub so the server can invoke it
        job.client.displayStatus = function () {
            getData();
        };

        // Start the connection
        $.connection.hub.start();
        getData();

    });
    function getData()
    {           
        var $tbl = $('#tbl');
        $.ajax({
            url: '/index.aspx/GetData',
            contentType: 'application/json;charset=utf-8',
            datatype: 'json',
            type: 'POST',     
            success: function (data) {
                if (data.d.length > 0) {

                    var newdata = data.d;
                    $tbl.empty();
                    $tbl.append(' <tr><th>ID</th><th>Name</th></tr>');
                    var rows = [];
                    for (var i = 0; i < newdata.length; i++) {
                        rows.push(' <tr><td>' + newdata[i].Id + '</td><td>' + newdata[i].Name + '</td><td></tr>');
                    }
                    $tbl.append(rows.join(''));                      
                }
            }
        });          
    }

</script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table id="tbl"></table>
            <dx:ASPxGridView ID="grid" runat="server"></dx:ASPxGridView>
        </div>
    </form>
</body>
</html>

However I want to use a Devepress Aspxgridview. The Devexpress Site states they don't support SignalR. However since the Javascript function is triggered when data changes in the database, is it possible somehow to force the client to get the data from the server? Force a postback and/or call the FillGrid method? ( To create the grid from js is not possible since the AspxgridView Control is much more complicated).

SOURCE: https://www.youtube.com/watch?v=30m-7wpmbrc

1

1 Answers

0
votes

Although SignalR is not supported out of the box, it should be possible to manually notify the server side about the update using the existing API. You can send a callback to the server using the ASPxClientGridView.PerformCallback method, and handle the server side ASPxGridView.CustomCallback event to reload data from the SQL server.