skip to Main Content

I have an ajax call to a vb function. The vb function fails because the dataTable the query alludes to TABLE_ONE does not exist in the database.

I want the error message from the catch in vb to be returned to the ‘error’ of the ajax so that I can display that error to the user. The reason being that the error message in VB states precisely that the table does not exist in the database, whereas the error that is actually displayed to the user from here

error: function (a, b, c) {
            alert("Ajax call to multiAjax failed: " + a);
        }

does not tell the user the error is due to a table that doesn’t exist. The ajax error message is useless.

How can I display the error message from the vb catch to the user?

Thanks

ajax:

function multiAjax(funcName, queryName, onSuccess) {
    var result = null;
    $.ajax({
        type: "POST",
        url: "WebService1.asmx/" + funcName,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (Response) {
            onSuccess(Response)
        },
        data: JSON.stringify({ "queryName": queryName }),
        error: function (a, b, c) {
            alert("Ajax call to multiAjax failed: " + a);
        }
    });
}

VB

<WebMethod(EnableSession:=True)>
    Public Function getQueryNo()
        Try
            Dim queryNumberSql As String
            Dim queryNo As Integer

            Using dr As New DataReader(dif)
                queryNumberSql =
                "SELECT min(unused) AS unused
            FROM (
                 SELECT MIN(t1.QUERY_NUMBER)+1 as unused
                 FROM TABLE_ONE AS t1
                 WHERE NOT EXISTS (SELECT * FROM TABLE_ONE AS t2 WHERE t2.QUERY_NUMBER = t1.QUERY_NUMBER+1)
                 UNION
                 -- Special case for missing the first row
                 SELECT 1
                 FROM TABLE_ONE AS t1
                 WHERE NOT EXISTS (SELECT * FROM TABLE_ONE WHERE QUERY_NUMBER = 1)
            ) AS subquery"

                dr.ExecuteReader(queryNumberSql)
                While dr.Read()
                    queryNo = Integer.Parse(dr("unused"))
                End While

                Return queryNo
            End Using
        Catch ex As Exception
            Console.WriteLine($"Error trying to set query number: {ex}")
            Return ex
        End Try
    End Function

2

Answers


  1. Chosen as BEST ANSWER

    Solution provided by @freedomn-m:

    ajax:

    function multiAjax(funcName, queryName, onSuccess) {
        var result = null;
        $.ajax({
            type: "POST",
            url: "WebService1.asmx/" + funcName,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (Response) {
                onSuccess(Response)
            },
            data: JSON.stringify({ "queryName": queryName }),
            error: function (jqXHR, textStatus, errorThrown) {           
                    alert(jqXHR.responseText);          
            }
        });
    }
    

    VB:

    Public Function getQueryNo()
            Try
                Dim queryNumberSql As String
                Dim queryNo As Integer
    
                Using dr As New DataReader(dif)
                    queryNumberSql =
                    "SELECT min(unused) AS unused
                FROM (
                     SELECT MIN(t1.QUERY_NUMBER)+1 as unused
                     FROM CFG_QUERY_REPORT AS t1
                     WHERE NOT EXISTS (SELECT * FROM CFG_QUERY_REPORT AS t2 WHERE t2.QUERY_NUMBER = t1.QUERY_NUMBER+1)
                     UNION
                     -- Special case for missing the first row
                     SELECT 1
                     FROM CFG_QUERY_REPORT AS t1
                     WHERE NOT EXISTS (SELECT * FROM CFG_QUERY_REPORT WHERE QUERY_NUMBER = 1)
                ) AS subquery"
    
                    dr.ExecuteReader(queryNumberSql)
                    While dr.Read()
                        queryNo = Integer.Parse(dr("unused"))
                    End While
    
                    Return queryNo
                End Using
            Catch ex As Exception
                HttpContext.Current.Response.StatusCode = 400
                HttpContext.Current.Response.Write(ex)
            End Try
        End Function
    

  2. This depends on your Library / Implementation you are using to make the request in the first place. Some libraries will reject if the server sends an error code, for example, 500.

    Whilst considered bad practice, sending an error code might be what you want. (Just don’t use this in an api!!)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search