0
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?

3
  • What does the string "1234123412341234" represent? Are you attempting to determine if a credit card number is valid? If so, there is no need to convert to int. Google for the Luhn algorithm. Commented Apr 28, 2017 at 19:56
  • 1
    These strings represent credit card numbers, right? So why are you trying to treat them as integers? It doesn't make sense to add credit card numbers together. Commented Apr 28, 2017 at 19:56
  • Indeed "-123456789012345" would fit the bill as "a 16 digit value that parses as an integer" but isn't a valid credit card number... Commented Apr 28, 2017 at 20:00

3 Answers 3

2

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

long num;
var works = long.TryParse(s, out num);
Sign up to request clarification or add additional context in comments.

4 Comments

How can I use long method with string and check if it contains only int values ?
@YamenMassalha: It's very unclear what you're asking, I'm afraid. This has nothing to do with ASP.NET, by the way.
Oh sorry for that (ASP.NET) :D @JonSkeet
max value of int: 2,147,483,647. max value of long: 9,223,372,036,854,775,807.
0

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";
}

Comments

0

With new C# 7.0 you can use the following syntax

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.