I work on asp.net mvc . i get error on action result prevent inserting but i don't know what is issue and why not inserting data . code working on my local pc but on server host app iis not work so how to know reason of issue can you please help me add try and catch and display this error on json result
public JsonResult RequesterIndex(ResignationRequester resignationRequester)
{
dynamic responseData = new ExpandoObject();
responseData.success = false;
responseData.message = "";
var filenumber = resignationRequester.EmpID;
if (Session[SessionKeys.UserCode] != null)
{
JDEUtility jde = new JDEUtility();
if (resignationRequester.othersInput != null)
{
resignationRequester.EmployeeRestrictions = resignationRequester.othersInput;
}
resignationRequester.DeptCode = Session[SessionKeys.DepartmentCode].ToString();
resignationRequester.EmpID = Convert.ToInt32(Session[SessionKeys.UserCode]);
resignationRequester.EmpName = Convert.ToString(Session[SessionKeys.Username]);
resignationRequester.Dept = Convert.ToString(Session[SessionKeys.Department]);
resignationRequester.Designation = Convert.ToString(Session[SessionKeys.Designation]);
resignationRequester.PayGrade = Convert.ToString(Session[SessionKeys.PayGrade]);
resignationRequester.CompanyCode = Convert.ToString(Session[SessionKeys.Company]);
resignationRequester.Gender = Convert.ToString(Session[SessionKeys.Gender]);
resignationRequester.ServiceDuration = Convert.ToString(Session[SessionKeys.ServiceDuration]);
int noticeperiod = Convert.ToInt32(Session[SessionKeys.NoticePeriod]);
int julianRequestDate = JulianDate.DateTimeToJulian(resignationRequester.LastWorkingDate);
int SubmitionDate = JulianDate.DateTimeToJulian((DateTime)Session[SessionKeys.SubmitionDate]);
int JoinedDate = JulianDate.DateTimeToJulian((DateTime)Session[SessionKeys.JoinDate]);
int storedLastedWorkingDate = JulianDate.DateTimeToJulian((DateTime)Session[SessionKeys.LastWorkingDate]);
if (ModelState.IsValid)
{
responseData.success = true;
responseData.message = "Resignation Submission form Created successfully";
}
else
{
responseData.success = false;
responseData.message = "Some Required Fields Not Added";
}
}
else
{
responseData.success = false;
responseData.message = "No Data For This File No";
}
return Json(responseData);
}
and call action result using ajax requestion with json result
$('#btnsubmit').click(function () {
var empidval = $("#txtempid").val();
$("#ResignationApp").submit(function (e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
dataType: 'json',
url: '@Url.Action("RequesterIndex", "Resignation")',
data: formData,
success: function (response) {
},
error: function (error) {
}
});
});
});
so how to know issue exception error from your opnion
Prasad RaveendranPosted Jan 24, 2024, 1:30 AM
Hello Ahmad,
Since you have confirmed that, your code works fine in your local. Please check the Application Pool which you have configured in IIS. Check the App Pool User has the access to the database where you are trying to insert records. I strongly believe, it could be a permission issue.
However, To catch and display the error on the JSON result, you can wrap the code in a try-catch block and set the error message in the
responseDataobject when an exception occurs. Here's an updated version of your code with a try-catch block:Lokesh VarmanPosted Jan 23, 2024, 2:54 AM
You can enhance your
RequesterIndexaction in ASP.NET MVC by adding a try-catch block to catch and handle any exceptions that might occur. Additionally, you can return the exception details along with the JSON response.Here's an example of how you can modify your action:
In your JavaScript code, you can log or display the error message:
This way, if an exception occurs on the server, you can easily identify the error message on the client side for debugging and troubleshooting.