How to resolve "Input string was not in the correct format" error

Simflex 301 Reputation points
2022-10-18T21:13:52.277+00:00

Greetings experts,

I have checked out some of the suggested solutions but none appears to help my problem.

I have a fieldname called caseNumber with Smallint data type in sql server database.

When I use the following line in my c# code:

if (caseNumber.Text != ""), I get the following error:

Input string was not in the correct format

I tried adding zero inside the quotes but same error.

Any ideas?

Many thanks in advance

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,397 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
326 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 50,591 Reputation points
    2022-10-18T21:29:48.173+00:00

    That particular line of code will not generate the error you're talking about. It is a simple string comparison. I'm making an assumption here that caseNumber is a web control, maybe a Textbox? Can you confirm?

    If it is a Textbox then that should just be retrieving the value as a string (everything is convertible to that) and then comparing it against an empty string. But what we cannot tell is if that control is actually something else (perhaps a third party library) or using something like the AJAX mask property or something. In those cases it is possible the property is trying to convert the actual value to a formatted value but that would seem odd since Text shouldn't be touched directly and is how the value would get back to the server anyway.

    Can you please provide more context around that particular field in your code? Additionally please post the exception details including the inner exception (if any) and the stack trace.

    0 comments No comments

  2. Simflex 301 Reputation points
    2022-10-18T21:32:24.397+00:00

    Hi @Michael Taylor ,

    Yes, it is correct that is a textbox.

    The error points to that line.

    Ok, we have a situation where you select user information and load that information on the screen.

    The screen has a prepopulated caseNumber to be associated with this user.

    However, when loading this particular user information to the screen, it comes blank. Not only does it not load the the screen, it erases the prepopulated caseNumber.

    An attempt to reload this user information into the screen, produces the "Input not in the correct format" error just because the prepopulated caseNumber on the screen was erased.

    FormtException; System,Number.StringToNumber...


  3. Lan Huang-MSFT 28,821 Reputation points Microsoft Vendor
    2022-10-19T02:22:46.047+00:00

    Hi @Simflex ,

    Input string was not in the correct format

    Based on the information you have provided so far, your data type is Smallint and you need to convert the specified value to an integer.
    https://video2.skills-academy.com/en-us/dotnet/api/system.convert.toint16?view=net-6.0
    E.g:

     int? Number = Convert.ToInt16(caseNumber.Text);  
                if (Number != null)  
                {  
      
                }  
    

    If you can, I suggest you provide more detailed information, such as a small example that reproduces the problem.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments