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.

You are viewing reply posted by: dharmendr one month ago.
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