Solved

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

Posted on 2009-07-07
3
715 Views
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)
    {
        runGeoCoding();
    }
 
    protected void runGeoCoding()
    {
        try
        {
            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];
            try
            {
                  
                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();
                    }
                    
                    
                }
                xmlreader.Close();
                yahoosr.Close();
                return coordList;
 
                
                
            }
            catch (Exception exgeolookup)
            {
                lbl_err.Text = exgeolookup.ToString();
                return coordList;
            }
    }

Open in new window

0
Comment
Question by:jwbrown65
3 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
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.
0
 
LVL 12

Accepted Solution

by:
ShazbotOK earned 500 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.
0
 

Author Closing Comment

by:jwbrown65
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.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connection String 16 54
Simple Injector with Web Service 4 39
FTP file download using c# 3 22
List<PaisEntity> - show some 1 24
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

805 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