0

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
Joe Joe Joe
  • 79
  • 1
  • 15
NickyLarson
  • 127
  • 1
  • 1
  • 12
  • VB version here: https://stackoverflow.com/a/2088134/2181514 – freedomn-m Dec 04 '19 at 17:30
  • Your "error message in the vb" is just a console.writeline - this is not transferable. You need to raise an exception with the correct message, eg `Catch ex as Exception Throw New ApplicationException("Error trying to set number", ex)` - however - this may get swallowed / hidden by your server-side configuration (web.config setting) - alternatively use Response.StatusCode/StatusDescription as in the link above. – freedomn-m Dec 04 '19 at 17:31
  • In addition, you're not actually looking at the useful parameter to `error:` - in your code `error:function(a,b,c) { alert(a)` look also at b and c, or, more specifically: `error: function(jqXHR, textStatus, errorThrown)` – freedomn-m Dec 04 '19 at 17:34
  • What is the value of a, b and c in the error handler on the client-side? – Lajos Arpad Dec 04 '19 at 17:56
  • @freedomn-m, thanks a lot! Response.StatusCode/StatusDescription worked. – NickyLarson Dec 05 '19 at 15:32

2 Answers2

0

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!!)

alistair
  • 565
  • 5
  • 15
  • I just want the error message from vb to be displayed to the user and there doesn't appear to be a straightforward way of doing this due to it being server side code – NickyLarson Dec 04 '19 at 17:25
0

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
NickyLarson
  • 127
  • 1
  • 1
  • 12