Convert string to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shaileshsharma
    New Member
    • Jun 2007
    • 26

    Convert string to integer

    Hi,
    I have a value in string type variable ,i want to convert the string type into integer.This is my sample code

    int s1;
    string s2;
    int s3;

    if (ds.Tables[0].Rows.Count > 0)
    {
    s1 = ds.Tables[0].Rows.Count;
    for (int i = 0; i < s1; i++)
    s2=(ds.Tables[0].Rows[i][0]).ToString();


    s3 = Convert.ToInt32 ("s2");


    i use also system.int32.pa rse

    but in both case the error is coming "Input string was not in a correct format."

    please help me to find out the answer of this problem

    Thanks,
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by Shaileshsharma
    Hi,
    I have a value in string type variable ,i want to convert the string type into integer.This is my sample code

    int s1;
    string s2;
    int s3;

    if (ds.Tables[0].Rows.Count > 0)
    {
    s1 = ds.Tables[0].Rows.Count;
    for (int i = 0; i < s1; i++)
    s2=(ds.Tables[0].Rows[i][0]).ToString();


    s3 = Convert.ToInt32 ("s2");


    i use also system.int32.pa rse

    but in both case the error is coming "Input string was not in a correct format."

    please help me to find out the answer of this problem

    Thanks,
    what is the value of s2 ???........you know that a string like "A01" cannot be converted to integer....

    Comment

    • blackjack2150
      New Member
      • Feb 2007
      • 79

      #3
      Originally posted by Shaileshsharma
      Hi,
      I have a value in string type variable ,i want to convert the string type into integer.This is my sample code

      int s1;
      string s2;
      int s3;

      if (ds.Tables[0].Rows.Count > 0)
      {
      s1 = ds.Tables[0].Rows.Count;
      for (int i = 0; i < s1; i++)
      s2=(ds.Tables[0].Rows[i][0]).ToString();


      s3 = Convert.ToInt32 ("s2");


      i use also system.int32.pa rse

      but in both case the error is coming "Input string was not in a correct format."

      please help me to find out the answer of this problem

      Thanks,
      Really silly bug.
      You're trying to convert the string "s2" to an integer.
      It should be

      s3 = Convert.ToInt32 (s2); // no quotes there

      Comment

      • mohitkatariya
        New Member
        • Apr 2007
        • 26

        #4
        Do no use quotes in this line
        s3 = Convert.ToInt32 ("s2");

        use:
        s3 = Convert.ToInt32 (s2);

        and check ur variable does not contain any alphab. or any other symbols, only numeric values can be converted.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by mohitkatariya
          Do no use quotes in this line
          s3 = Convert.ToInt32 ("s2");

          use:
          s3 = Convert.ToInt32 (s2);

          and check ur variable does not contain any alphab. or any other symbols, only numeric values can be converted.
          it IS possible to parse other strings if you need to, check into System.Globaliz ation.NumberSty les if you want to see more examples of how to parse the string "$1,000.00" or a hex number like "A23B"

          Comment

          Working...