float.Parse results in Input string was not in a correct format.

Posted on 2009-07-07
Medium Priority
Last Modified: 2012-06-27
Having some trouble with parsing the results of a geocode lookup from Yahoo and storing the coordinates as a float by means of the float.Parse method.

When I use any generic example the float.Parse works fine:
str lng = "113.995704";
float mylng = float.Parse(lng);
lbl_results.Text = mylng;

If I print the results of the Yahoo coordList[] straight to a label on the screen, I have no problems.  When I use a similar example to the code below with Google's implementation, the parsing function on the coordList[] also works fine.  The challenge comes in when I use the parse function on the coordList[] from Yahoo data (as I need to store the lat and lng as float in the DB - client requirement).

Any help on how to manipulate the results to work in the float.Parse function (or case the data to float in some other fashion) would be appreciated.

protected void Button1_Click(object sender, EventArgs e)
    protected void runGeoCoding()
            string[] geoValues = geoLookup(txtAddress.Text, txtCity.Text, txtState.Text, txtZip.Text);
            float geoCodeLong = float.Parse(geoValues[0]);
            float geoCodeLat = float.Parse(geoValues[1]);
            lbl_results.Text = geoValues.Length + " " + geoCodeLong + " , " + geoCodeLat;
        catch (Exception rungeo)
            lbl_err.Text = rungeo.ToString();
    private string[] geoLookup(string address, string city, string state, string zip)
            string[] coordList = new string[2];
                string yahoourl = "http://local.yahooapis.com/MapsService/V1/geocode?appid=thisappid&street=" + System.Web.HttpUtility.UrlEncode(address) + "&city=" + System.Web.HttpUtility.UrlEncode(city) + "&state=" + System.Web.HttpUtility.UrlEncode(state) + "&zip=" + System.Web.HttpUtility.UrlEncode(zip);
                WebRequest yahooreq = HttpWebRequest.Create(yahoourl);
                WebResponse yahoores = yahooreq.GetResponse();
                StreamReader yahoosr = new StreamReader(yahoores.GetResponseStream());
                XmlTextReader xmlreader = new XmlTextReader(yahoosr);
                while (xmlreader.Read())
                    if (xmlreader.NodeType == XmlNodeType.Element && xmlreader.Name == "Latitude")
                        coordList[1] = xmlreader.ReadOuterXml();
                    if (xmlreader.NodeType == XmlNodeType.Element && xmlreader.Name == "Longitude")
                        coordList[0] = xmlreader.ReadOuterXml();
                return coordList;
            catch (Exception exgeolookup)
                lbl_err.Text = exgeolookup.ToString();
                return coordList;

Open in new window

Question by:jwbrown65
LVL 40

Expert Comment

ID: 24797985
Without seeing the data, I am wondering if there is some other characters in the strings. Add a catch{} section and print the values to see.
LVL 12

Accepted Solution

ShazbotOK earned 2000 total points
ID: 24798001
it is entirely possible that Yahoo (being who they are) is passing some invalid char values along with the float value that does not display in a simple label...  I would attempt to clean the string values like so:
            float geoCodeLong = float.Parse(geoValues[0].Trim());
            float geoCodeLat = float.Parse(geoValues[1].Trim());
If that doesnt do it then you may want to debug the values and see what the debugger view can show you on the values in the array...  
Also the .TryParse() is a good function to use if you don't want to have to catch the exceptions.

Author Closing Comment

ID: 31600778
Was originally troubleshooting through the entire app, but decided to break off this piece of code to a local instance of the debugger.  Lo and behold, Yahoo was passing a little more data than the program anticipated so the float.Parse was failing on the extra info.  Thanks for the idea.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question