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 ChhayaRank #304 · 8.2K reputationPosted Nov 5, 2024, 7:49 AM EST
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:
if (!string.IsNullOrEmpty(OrderNumber) && OrderNumber != "null") { int orderId; if (int.TryParse(OrderNumber, out orderId)) { modifiedData = modifiedData .Where(a => a.OrderId == orderId) .OrderBy(x => x.OrderDate) .ThenBy(x => x.Qty) .ToList(); } else { // Handle the case where OrderNumber is not a valid integer // For example, log the error or return a message to the user } } 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) x.OrderDate) .ThenBy(x => x.Qty) .ToList(); }Naveen KumarRank #249 · 10K reputationPosted Nov 5, 2024, 7:23 AM EST
Hi Emmmanuel,
Please check the below code.
if (!string.IsNullOrEmpty(OrderNumber) && OrderNumber != "null") { // Attempt to parse OrderNumber as an integer if (int.TryParse(OrderNumber, out int orderNumberInt)) { modifiedData = modifiedData .Where(a => a.OrderId == orderNumberInt) .OrderBy(x => x.OrderDate) .ThenBy(x => x.Qty) .ToList(); } else { // Handle the case where OrderNumber is not a valid integer // You might log this as an error, throw an exception, or ignore this filter // For example: throw new FormatException("OrderNumber is not in a valid integer format."); } } if (!string.IsNullOrEmpty(sDate) && !string.IsNullOrEmpty(eDate)) { // Parse the dates DateTime fromDate = DateTime.Parse(sDate); DateTime toDate = DateTime.Parse(eDate); modifiedData = modifiedData .Where(a => DateTime.Parse(a.OrderDate) >= fromDate && DateTime.Parse(a.OrderDate) x.OrderDate) .ThenBy(x => x.Qty) .ToList(); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 5, 2024, 9:14 AM EST
Hello Team,
Thank you for your swift response but however the this the error that came after implementing both format you posted here please.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 6, 2024, 2:45 AM EST
Hi Emmmanuel FIADUFE,
Try the updated code
We can enhance the validation of the
OrderNumberbefore attempting to parse it.if (!string.IsNullOrEmpty(OrderNumber) && OrderNumber.Trim() != "null") { // Attempt to parse OrderNumber as an integer if (int.TryParse(OrderNumber, out int orderNumberInt)) { modifiedData = modifiedData .Where(a => a.OrderId == orderNumberInt) .OrderBy(x => x.OrderDate) .ThenBy(x => x.Qty) .ToList(); } else { // Log the error or handle it appropriately throw new FormatException("OrderNumber is not in a valid integer format."); } }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 6, 2024, 6:37 AM EST
Hello Jayraj
Please this is the error I encounter again.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 6, 2024, 7:04 AM EST
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.