VB.Net Integer.TryParse Error: Input string was not in a correct format

Last Reply one month ago By dharmendr

Posted one month ago

This is the Number 05-07152 i want to pass please help

      Integer.TryParse(context.Request.QueryString("customerId"), customerId)

Please help me out i wanted to pass this number but i get error System.FormatException: Input string was not in a correct format.

Posted one month ago

Hi

Will you please let me know what exact result you want to get from the number 05-07152.


Posted one month ago

I wanted to retain that very number as it is  after requesting it 


Posted one month ago

Hi makumbi,

You can't convert the number with special character to integer using Integer.TryParse is not possible.

You can handle the error using the below way.

C#

int customerId = 0;
string value = "05-07152";
bool success = int.TryParse(value, out customerId);
if (success)
{
    Response.Write(String.Format("Converted <b>{0}</b> to <b>{1}</b>.", value, customerId));
}
else
{
    Response.Write(String.Format("Conversion of <b>{0}</b> to int failed.", value));
}

VB.Net

Dim customerId As Integer = 0
Dim value As String = "05-07152"
Dim success As Boolean = Integer.TryParse(value, customerId)
If success Then
    Response.Write(String.Format("Converted <b>{0}</b> to <b>{1}</b>.", value, customerId))
Else
    Response.Write(String.Format("Conversion of <b>{0}</b> to int failed.", value))
End If