Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Show Asp.Net Exception Error Message in AJAX Call Error Function in C#.Net VB.Net

May 17, 2015
Introduction

Here I will explain how to show
asp.net exception error message in jQuery ajax web method calls using error function in c#, vb.net or get error message in jQuery ajax post method in asp.net using c#, vb.net with example or jQuery show custom error message during ajax calls in asp.net using c#, vb.net.


Now create one new web application and open your aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery Get Original Asp.net Exception Message in Ajax Method</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#btnSum').click(function () {
var val1 = $.trim($('#txtVal1').val());
var val2 = $.trim($('#txtVal2').val());
$.ajax({
type: "POST",
url: "GetExceptionMessageinjQuery.aspx/GetSumofNumbers",
data: "{'val1':'" + val1 + "', 'val2':'" + val2 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#lblMessage').text(response.d)
},
error: function (data) {
var r = jQuery.parseJSON(data.responseText);
var errorMessage = r.Message;
var exceptionType = r.ExceptionType;
var stackTrace = r.StackTrace;
$('#divStatus').html("<b>Error Message: </b>" + errorMessage + "</br></br>" + "<b>ExceptionType: </b>" + exceptionType + "</br></br>" + "<b>Asp.Net StackTrace: </b>" + stackTrace)
}
});
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>First Number:</td><td><input type="text" id="txtVal1" /></td></tr>
<tr><td>Second Number:</td><td><input type="text" id="txtVal2" /> </td></tr>
<tr><td>SUM:</td><td><label id="lblMessage"/></td></tr>
<tr><td colspan="2"><input type="button" id="btnSum" value="Get Sum" /></td></tr>
</table>
<hr />
<div id="divStatus"></div>
</div>
</form>
</body>
</html>

Now open aspx page codebehind behind file and add following namespaces

C# Code


using System;
using System.Web.Services;

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{

}
[WebMethod]
public static int GetSumofNumbers(int val1, int val2)
{
return val1 + val2;
}

VB.NET Code


Imports System.Web.Services
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
<WebMethod()> _
Public Shared Function GetSumofNumbers(ByVal val1 As Integer, ByVal val2 As Integer) As Integer
Return val1 + val2
End Function
End Class

Demo


jQuery Show Asp.Net Exception Error Message in AJAX Call Error Function in C#.Net VB.Net

Download Sample Code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.