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.
Description:
In previous articles I explained jQuery insert data into database without postback in asp.net, jQuery show hide div based on checkbox selection, bind dropdownlist in gridview using dataset in asp.net, import data from excel to database in asp.net, read xml file and bind data to gridview in asp.net, Delete multiple rows in gridview using checkbox in asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to get original asp.net exception message in jQuery Ajax calls in c#,vb.net.
In previous articles I explained jQuery insert data into database without postback in asp.net, jQuery show hide div based on checkbox selection, bind dropdownlist in gridview using dataset in asp.net, import data from excel to database in asp.net, read xml file and bind data to gridview in asp.net, Delete multiple rows in gridview using checkbox in asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to get original asp.net exception message in jQuery Ajax calls in 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
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.