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

Simflex 321 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

Developer technologies ASP.NET Other
Developer technologies .NET .NET CLI
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 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 321 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 30,186 Reputation points Microsoft External Staff
    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://learn.microsoft.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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.