Hello Team,
In my MVC controller I keep having this error in this code Convert.ToInt32(OrderNumber), which says that the Input string is not in correct format, and I realised it in my Sql Database table i used the data type as Varchar so I tried to re-write the code as Convert.ToString(OrderNumber).ToList() but this one to didn't work, kindly assist please.
if (!string.IsNullOrEmpty(OrderNumber) && OrderNumber != "null")
{
modifiedData = modifiedData.Where(a => a.OrderId == Convert.ToInt32(OrderNumber)).OrderBy(x => x.OrderDate).ThenBy(x => x.Qty).ToList();
}
if (!string.IsNullOrEmpty(sDate) && !string.IsNullOrEmpty(eDate))
{
DateTime fromDate = DateTime.Parse(sDate);
DateTime toDate = DateTime.Parse(eDate);
modifiedData = modifiedData.Where(a => DateTime.Parse(a.OrderDate) >= fromDate && DateTime.Parse(a.OrderDate) <= toDate).OrderBy(x => x.OrderDate).ThenBy(x => x.Qty).ToList();
}
Jayraj ChhayaPosted Nov 5, 2024, 12:49 PM
Hello,
The error you are experiencing arises because the
OrderNumberis stored as avarcharin your SQL database, which may contain non-numeric characters or be empty. To resolve this, you should first ensure that theOrderNumbercan be safely converted to an integer. Instead of usingConvert.ToInt32, consider usingint.TryParse, which allows you to handle conversion failures gracefully.Here’s a revised version of your code:
Jayraj ChhayaPosted Nov 6, 2024, 12:04 PM
Hi Emmmanuel FIADUFE,
Could you share a screenshot or details of the `OrderNumber` variable values you received? It looks like there might be an invalid value, like a character or another non-numeric entry, which could be causing an error when parsing to an integer.
Please share the details as a screenshot or a code snippet.
Emmmanuel FIADUFEPosted Nov 6, 2024, 11:37 AM
Hello Jayraj
Please this is the error I encounter again.
Jayraj ChhayaPosted Nov 6, 2024, 7:45 AM
Hi Emmmanuel FIADUFE,
Try the updated code
We can enhance the validation of the
OrderNumberbefore attempting to parse it.Emmmanuel FIADUFEPosted Nov 5, 2024, 2:14 PM
Hello Team,
Thank you for your swift response but however the this the error that came after implementing both format you posted here please.
Naveen KumarPosted Nov 5, 2024, 12:23 PM
Hi Emmmanuel,
Please check the below code.