I work on asp.net mvc application . i get error input string not in correct format
i don't know what is the reason of this issue and which line give me this issue
on local pc issue not happen but on iis publish site web app it happen
so how i know reason of this issue please
and can i do some enhancement on code to display more clear message or prevent this error from happen
on which line
my action code as below
public JsonResult RequesterIndex(ResignationRequester resignationRequester)
{
dynamic responseData = new ExpandoObject();
responseData.success = false;
responseData.message = "";
try
{
var filenumber = resignationRequester.EmpID;
if (Session[SessionKeys.UserCode] != null)
{
JDEUtility jde = new JDEUtility();
resignationRequester.EmpID = Convert.ToInt32(Session[SessionKeys.UserCode]);
resignationRequester.Gender = Convert.ToString(Session[SessionKeys.Gender]);
if (ModelState.IsValid)
{
if (Convert.ToString(resignationRequester.LineManager).Length < 6 && !string.IsNullOrEmpty(resignationRequester.LineManager.ToString()))
{
responseData.success = false;
responseData.message = "Length Line Manager Must Be equal 6 or More";
return Json(responseData);
}
if (Convert.ToString(resignationRequester.DirectManager).Length < 6 && !string.IsNullOrEmpty(resignationRequester.DirectManager.ToString()))
{
responseData.success = false;
responseData.message = "Length Direct Manager Must Be equal 6 or More";
return Json(responseData);
}
if (Convert.ToInt32(resignationRequester.EmpID) == Convert.ToInt32(resignationRequester.DirectManager) &&
Convert.ToInt32(resignationRequester.EmpID) == Convert.ToInt32(resignationRequester.LineManager)
&& !string.IsNullOrEmpty(Convert.ToString(resignationRequester.LineManager)) && !string.IsNullOrEmpty(Convert.ToString(resignationRequester.DirectManager)))
{
responseData.success = false;
responseData.message = "Requester Have Same Number of Manager";
return Json(responseData);
}
if (!string.IsNullOrEmpty(Convert.ToString(resignationRequester.LineManager)))
{
responseData.success = false;
responseData.message = "Line Manager Name Blank";
return Json(responseData);
}
if (string.IsNullOrEmpty(ViewBag.errorMsg))
{
responseData.success = true;
responseData.message = "Resignation Submission form Created successfully";
TempData["SuccessBeforePrint"] = 1;
return Json(responseData);
}
}
else
{
responseData.success = false;
var errors = ModelState.Select(x => x.Value.Errors)
.Where(y => y.Count > 0)
.ToList();
responseData.message = "Some Required Fields Not Added";
return Json(responseData);
}
}
}
catch (System.FormatException ex)
{
responseData.success = false;
responseData.message = ex.Message;
}
return Json(responseData);
}
on ui this is what i do
$("#txtLineManagerId").autocomplete({
source: function (request, response) {
var searchText = $("#txtLineManagerId").val();
console.log("search text" + searchText)
$.ajax({
url: '@Url.Action("GetAllEmployeeBasedSearchText", "Resignation")',
data: { searchText: searchText },
method: "GET",
dataType: "json",
success: function (data) {
if (!data.length) {
var result = [
{
label: 'No matches found',
value: response.term
}
];
response(result);
}
else {
response($.map(data, function (item) {
return {
label: "File No : " + item.EmployeeID + " - " + "Name :" + item.EmployeeName + " - " +
"Designation : " + item.Designation, value: item.EmployeeID,
employeeName: item.EmployeeName,
designation: item.Designation
};
}))
}
}
});
},
position: { my: "right top", at: "right bottom" },
appendTo: '#searchContainer',
select: function (event, ui) {
$("#LineManagerName").val(ui.item.employeeName);
},
minLength: 2,
}).data("ui-autocomplete")._renderItem = function (ul, item) {
console.log("after autocomplete" + item);
return $("")
.append("File No: " + item.value + "
Name: " + item.employeeName + "
Designation: " + item.designation + "")
.appendTo(ul);
};
@Html.LabelFor(model => model.LineManager, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.LineManager, new
{
htmlAttributes = new
{
@class = "form-control"
,
id = "txtLineManagerId"
}
})
public class ResignationRequester
{
[Display(Name = "Employee No : ")] [Required,]
public int EmpID { get; set; }
[Display(Name = "Line Manager: ")]
public int? LineManager { get; set; }
}
Prasad RaveendranPosted Jan 24, 2024, 1:18 AM
The error "input string not in correct format" typically occurs when there is an attempt to convert a string to a numeric type, and the string does not represent a valid number. In your code, you are using
Convert.ToInt32in several places, and the issue might be related to the data you are trying to convert.Here are a few suggestions to troubleshoot and resolve the issue:
Check the Data Types:
resignationRequester.EmpID,resignationRequester.LineManager, andresignationRequester.DirectManagerare all of the expected types (integers in this case).Handle Nullable Types:
ResignationRequesterclass,LineManageris defined asint?(nullable integer). Make sure that you handle null values appropriately, especially when comparing or converting.Validate Input:
int.TryParsefor this purpose. If parsing fails, handle it accordingly.4. Check Session Values:
Sessionvariables (Session[SessionKeys.UserCode]and others) are valid and represent numeric values before using them in conversions.5. Debugging:
6. Handle Exceptions:
System.FormatExceptionis a good start.By carefully inspecting the data types, handling nullable types, validating input, and using debugging tools, you should be able to identify and resolve the issue causing the "input string not in correct format" error.