Yamen Massalha
Yamen Massalha

Reputation: 3

C# ASP.NET Int.TryParse method

public string AddToVisa(string s, string s1)
{
    int num;
    int num1;
    Functions con1 = new Functions();
    SqlConnection con = con1.get();
    if (s.Length != 16)
    {

        return "Wrong Details";

    }

        if (!(int.TryParse(s, out num)) || !(int.TryParse(s1, out num1)))
        {
            return "Wrong Visa Details";

        }


    if (s1.Length != 3)
    {
        return "Wrong Visa Details";
    }

    return "Done";

The function always returns "Wrong Details" (I tried with these values : s: 1234123412341234 , s1: 123) , The problem is from int.TryParse , When I deleted it the function returned "Done", What's the problem with int.TryParse?

Upvotes: 0

Views: 1535

Answers (3)

Utkarsh Bais
Utkarsh Bais

Reputation: 197

With new C# 7.0 you can use the following syntax

var works = long.TryParse(s, out long num);

Upvotes: 0

Rahul
Rahul

Reputation: 77934

You say The function always returns "Wrong Details"... then it seems there is space in your string variable string s. Try using Trim() before checking the Length property like

if (s.Trim().Length != 16)
{
    return "Wrong Details";
}

Upvotes: 0

The max value of int is 2,147,483,647. Try use long.

long num;
var works = long.TryParse(s, out num);

Upvotes: 2

Related Questions