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
C#
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 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
JavaScript
$("#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"
}
})C#
public class ResignationRequester
{
[Display(Name = "Employee No : ")] [Required,]
public int EmpID { get; set; }
[Display(Name = "Line Manager: ")]
public int? LineManager { get; set; }
}
Prasad RaveendranRank #320 · 7.8K reputationPosted Jan 23, 2024, 8:18 PM EST
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.if (!int.TryParse(resignationRequester.LineManager, out int lineManagerId)) { responseData.success = false; responseData.message = "Invalid Line Manager ID format"; return Json(responseData); }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.try { // ... your existing code ... } catch (Exception ex) { responseData.success = false; responseData.message = ex.Message; // Log the exception for further investigation. }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.