0

Creating: A handler which checks in database if there's a new messsage.

What I've done so far: Javascript that using AJAX runs a handler every third second.

What I'm unable to do: Returning a result from ASHX handler to AJAX javascript so that I can perform a postback if the result is true.

Here's the Javascript:

    //handle message upload

        window.setInterval(function () {

            $.ajax({
                url: "handlecb.ashx",
                type: "POST",

                success: function (result) {
                    toastr.options = {
                        "closeButton": false,
                        "debug": false,
                        "newestOnTop": true,
                        "progressBar": false,
                        "positionClass": "toast-bottom-left",
                        "preventDuplicates": true,
                        "onclick": null,
                        "showDuration": "1300",
                        "hideDuration": "1300",
                        "timeOut": "5300",
                        "extendedTimeOut": "1000",
                        "showEasing": "swing",
                        "hideEasing": "linear",
                        "showMethod": "fadeIn",
                        "hideMethod": "fadeOut"
                    }
                    toastr.info('Online.');

                   if (result < 0) {
                        __doPostBack("<%= senbut.ClientID %>", "");
                        toastr.options = {
                            "closeButton": false,
                            "debug": false,
                            "newestOnTop": true,
                            "progressBar": false,
                            "positionClass": "toast-bottom-left",
                            "preventDuplicates": true,
                            "onclick": null,
                            "showDuration": "1300",
                            "hideDuration": "1300",
                            "timeOut": "5300",
                            "extendedTimeOut": "1000",
                            "showEasing": "swing",
                            "hideEasing": "linear",
                            "showMethod": "fadeIn",
                            "hideMethod": "fadeOut"
                        }
                        toastr.success('New message recieved.');
                    }


                },
                error: function (err) {
                    toastr.options = {
                        "closeButton": false,
                        "debug": false,
                        "newestOnTop": true,
                        "progressBar": false,
                        "positionClass": "toast-bottom-left",
                        "preventDuplicates": true,
                        "onclick": null,
                        "showDuration": "1300",
                        "hideDuration": "1300",
                        "timeOut": "5300",
                        "extendedTimeOut": "1000",
                        "showEasing": "swing",
                        "hideEasing": "linear",
                        "showMethod": "fadeIn",
                        "hideMethod": "fadeOut"
                    }
                    toastr.danger('Offline');

                }
            });
        }, 3000);

Here's the Handler:

    <%@ WebHandler Language="VB" Class="handlecb" %>

    Imports System.Data
    Imports System.IO
    Imports System.Data.OleDb
    Imports System.Web.SessionState
    Imports System.Web.Security
    Imports System.Web

        Public Class handlecb
            Implements IHttpHandler, System.Web.Session

State.IRequiresSessionState

    Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ishaan Patel\Desktop\Paperhome\paperhome_data.accdb")


Dim cmd As OleDbCommand
Dim cmd2 As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim dr As OleDbDataReader
Dim result As Integer

Public Sub ProcessRequest(ByVal context As HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
    Dim threadid As String
    If Not context.Session("chatsess") Is Nothing Then
        threadid = context.Session("chatsess")
        GoTo b
    Else
        GoTo a
    End If
    b:
    Dim thread As Integer
    thread = Convert.ToInt32(threadid)


    con.Open() 'checklastupdatedon
    cmd = New OleDbCommand("SELECT [last_updated_on] FROM [message_threads] WHERE ([message_threads].[thread_id] = " & thread & ")", con)
    cmd.Connection = con
    dr = cmd.ExecuteReader
    If (dr.Read) Then
        Dim fetched As Date = Convert.ToDateTime(dr("last_updated_on"))
        Dim old As Date = Convert.ToDateTime(context.Session("last_updated_on"))

        result = DateTime.Compare(old, fetched)
        con.Close()

        If result < 0 Then 'old fetched is earlier than timer fetched

            context.Session("last_updated_on") = fetched

        End If

    End If
    a:
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get




   Return result
    End Get
End Property
 End Class

Any help/suggestion is appreciated. Thank you.

1 Answers1

0

The function name is missing while calling the handler. Please change the code as follows :

url: "handlecb.ashx/ProcessRequest",
type: "POST",
Rahul Jain
  • 3,130
  • 1
  • 14
  • 16
  • Did that. It still won't notify when a new message is recieved. I tried the logic on a button click and it works fine, so I am sure the handler isnt the problem. Is the way I'm returning result correct? – Ishaan Patel Apr 03 '16 at 07:53
  • Please post the response you are getting after button click. – Rahul Jain Apr 03 '16 at 08:17
  • There's no button click that's happening or is supposed to happen. All of this is supposed to work without any kind of button click. Although, clearing out- The code within result < 0 is executed. Guide me in a way that enables me to fix a value inside this block and send that same value to the javascript from the handler. – Ishaan Patel Apr 03 '16 at 10:33