C# Input string not in correct format

Discussion in 'Software' started by mastermosley, Feb 27, 2010.

  1. mastermosley

    mastermosley Sergeant

    I loaded a string from a xml node.

    string aa = nodes.Childnodes[3].Innertext;

    Now if I use the string as is, say in a messagebox or displayed in a textbox
    at runtime the strings contents are displayed properly, (its a number), when I try,

    int aaa = Convert.ToInt32(aa);

    I get Input string not in correct format, wtf?
     
  2. mastermosley

    mastermosley Sergeant

    When I debug it, I set it to stop right before the string to int conversion it tells me that "754" is in it. So I am clueless of why im getting an error
    .
     
  3. SWario

    SWario Sergeant

    The only thing I can think of is that the string contains quotes in it, but that seems unlikely. Can you post a larger segment of code for us to look at?
     
  4. Wyatt_Earp

    Wyatt_Earp MajorGeek

    It could also be that there is a leading or trailing space. Sometimes those are hard to see. Try Convert.ToInt32(aa.Trim());
     
  5. mastermosley

    mastermosley Sergeant

    No spaces and no quotations
    Code:
      foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                    {
    
                        
                        
    
                       
    
                        string[] row = { node.ChildNodes[1].InnerText, node.Attributes["name"].Value , node.ChildNodes[0].InnerText, node.ChildNodes[3].InnerText,   };
                        string aa = row[3].Trim();
                        string bbs = "776";
                        int bb = Convert.ToInt32(aa);
                        datagrid.Rows.Add(row);
                        
                       
                    }
    
    In my code I'm taking the string from a string array, but if I go,
    string aa = node.Childnodes[3].InnerText; I still get the same error. The
    bbs is just a random string I added to compare the two at runtime and they
    both look exactly the same in the text viewer at runtime...

    Note, I'm taking that perticular string out because I need to do calculations
    with that number and it will be displayed in the grid.

    Doesnt work when I use int.Parse either, I get the same error
     
    Last edited: Mar 3, 2010
  6. mastermosley

    mastermosley Sergeant

    I added

    string aa = row[3].Trim();
    string ac = aa.ToLower();

    int bb = int.Parse(ac);


    if (a == 0)
    {
    lblSkillName.Text = "Attack";
    foreach (XmlNode node in doc.DocumentElement.ChildNodes)
    {






    string[] row = { node.ChildNodes[1].InnerText, node.Attributes["name"].Value , node.ChildNodes[0].InnerText, node.ChildNodes[3].InnerText, };
    string aa = row[3].Trim();
    string ac = aa.ToLower();

    int bb = int.Parse(ac); <---------- STOPPED HERE
    datagrid.Rows.Add(row);


    }

    Now it doesnt give me the error right away, it stops first time with no error
    I push run again and it stops no error has the right numbers, (its reading from an xml database with only 3 entries at the moment) does it again no error, but the loop should stop but it goes one more time and the Input string error is called with the string having null in it.


    **I added a if (aa != "") before the conversion and it works now, but I would still like to no why I was getting an Input string not in correct format error
     
  7. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Can you post the XML file that you're using to test with?

    I seem to remember having a problem where there were some extra characters after a string, but they didn't show up when I hovered my mouse over the variable while debugging. Is it possible that you may have a similar problem?
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds