4 Replies - 5671 Views - Last Post: 21 December 2011 - 08:19 AM Rate Topic: -----

#1 Nick86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 11

C# String Split, int.parse

Posted 11 February 2011 - 02:41 AM

Hi,

I keep getting the following error message:

"Input string was not in a correct format."

The Error message points to this line of code:

stadiumid = int.Parse(getfileNameUpdate[1]);

here is the full code:

        public void AddStadiumsToDB(string[] stadiums, string donepath)
        {
            int stadiumid = 0;

            foreach (string stadia in stadiums)
            {
                string[] getFileName = stadia.Split('\\');
                string[] getfileNameUpdate = getFileName[getFileName.GetUpperBound(0)].Split('.');
                string[] getfileupdate = getFileName[0].Split('{','}');
                string movepath = donepath + getFileName[getFileName.GetUpperBound(0)];
                string movepathUpdate = donepath + getfileNameUpdate[0];
                stadiumid = int.Parse(getfileNameUpdate[1]);
                addSpreadsheetDataToDb(stadia,stadiumid);
                File.Move(stadia, movepath);
            }
        }


Any help would be appreciated

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 11 February 2011 - 04:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C# String Split, int.parse

#2 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: C# String Split, int.parse

Posted 11 February 2011 - 04:24 AM

maybe try
Convert.ToInt32(getfileNameUpdate[1]);
:)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,603
  • Joined: 23-August 08

Re: C# String Split, int.parse

Posted 11 February 2011 - 04:34 AM

You're getting the error because...hold onto your hat...the value in getFileNameUpdate[1] is...wait for it...NOT AN INTEGER! My first suggestion is to find out why that's the case; where we have no example data to work from, we can't help you there. After you find out why that's the case, you should handle that as you think is appropriate in the code. One way to do this is using TryParse:

int myInt;
string badInput = "bad";
string goodInput = "42";
if (!int.TryParse(badInput, out myInt))
{
    Console.WriteLine("{0} was not an integer", badInput);
}

if (!int.TryParse(goodInput, out myInt))
{
    // This won't happen
}

Console.WriteLine("You entered {0}", myInt);


Was This Post Helpful? 2
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: C# String Split, int.parse

Posted 11 February 2011 - 05:53 AM

Put a breakpoint where you are converting to an integer and check what value that string is holding. Is it a number? Or is it something like, "76//"? More likely than not your string is being split wrong.
Was This Post Helpful? 0
  • +
  • -

#5 metinc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-December 11

Re: C# String Split, int.parse

Posted 21 December 2011 - 08:19 AM

Thx guys, this helps me to split my text codes ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1