Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:04:45+00:00 2026-06-06T20:04:45+00:00

I need to parse a string from a datatable into a double. I have

  • 0

I need to parse a string from a datatable into a double. I have a datatable (dt) and it has different columns, and each of these columns are converted to strings with the .ToString() method. However, when I try to output the latitude and longitude of the data, I get an error “Input string was not in a correct format.” So I guess my main question is, how do you parse text from an object converted to a string using the .ToString() method..

using System;
using System.IO;
using System.Collections;
using System.Data;

namespace SalesMap
{
    class SalesMap
    {
        private static DataTable InitData1()
        {
            //Datatable for entire list of zipcodes
            DataTable datat = new DataTable("DATA");
            DataColumn state = new DataColumn();
            DataColumn county = new DataColumn();
            DataColumn statecounty = new DataColumn();
            DataColumn latitude = new DataColumn();
            DataColumn longitude = new DataColumn();
            DataColumn salesperson = new DataColumn();

            //Add the columns to the datatable
            datat.Columns.Add(state);
            datat.Columns.Add(county);
            datat.Columns.Add(statecounty);
            datat.Columns.Add(latitude);
            datat.Columns.Add(longitude);
            datat.Columns.Add(salesperson);

            return datat;
        }       

        private static String InitPath()
        {
            string path = "C:/Documents and Settings/Andre/Desktop/salesmapdata/MapPointCountyLatLong.csv";
            return path;
        }

        public static void Main()
        {
            try
            {
                DataTable dt = InitData1();
                StreamReader sr = new StreamReader(InitPath());
                String csvData = string.Empty;

                while ((csvData = sr.ReadLine()) != null)
                {
                    String[] data = csvData.Split(',');
                    //dt.Rows.Add(data);
                    DataRow newRow1 = dt.NewRow();
                    newRow1[0] = data[0].ToString(); //state
                    newRow1[1] = data[1].ToString(); //county
                    newRow1[2] = data[2].ToString(); //state|county
                    newRow1[3] = data[3].ToString(); //latitude
                    newRow1[4] = data[4].ToString(); //longitude
                    newRow1[5] = data[5].ToString(); //salesperson
                    dt.Rows.Add(newRow1);

                    Console.WriteLine("Row added for: dt");
                }

                foreach (DataRow row1 in dt.Rows)
                {
                    double latitude1 = Double.Parse(row1[3].ToString());
                    double longitude1 = Double.Parse(row1[4].ToString());

                    if (row1[5].ToString() != "UNKNOWN" || row1[5].ToString() != "SALESMAN")
                    {
                        Console.WriteLine(latitude1);
                        Console.WriteLine(longitude1);
                    }
                }

                dt.WriteXml(@"c:\test\dt1.xml");
                sr.Close();
            }

            catch (Exception e)
            {
                StreamWriter sw = new StreamWriter(@"c:\test\error.txt");
                sw.WriteLine(e.Message.ToString());
                sw.Close();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:04:47+00:00Added an answer on June 6, 2026 at 8:04 pm

    First of all, when reading data from CSV file, you don’t need to call data[x].ToString() on each array element, since they are already strings.

    Few potential issues you might have with Double.Parse calls:

    1. The data in your CSV file is really in the wrong format (maybe you
      misplaced the column numbers, or data might have an additional symbols that couldn’t be parsed, like Tim mentioned in his comment).
    2. You have empty strings in your columns. Those cannot be parsed to double. You need to check that first, using string.IsNullOrWhitespace() or something similar.
    3. Your culture is wrong. You might have latitude and longitude values that use decimal point in your CSV file, but your current thread culture might be using comma as decimal separator. There’s an overload of Decimal.Parse() that accepts Culture as parameter (you could supply CultureInfo.InvariantCulture or any other CultureInfo).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website where I need to parse date/time strings from receipts. These
I need to parse some string from an input file. These string are needed
I need to parse string from datapicker to a DataTime object and save in
I have some sort of recursive function, but I need to parse a string,
I need to parse out the string that has following structure x:{a,b,c,}, y:{d,e,f} etc.
I have a large string I need to parse, and I need to find
I need to parse strings inputs where the columns are separated by columns and
I need to parse complex expresion from string to bool. It can only contain:
I need to parse the domain name from a string. The string can vary
I need to parse out time tokens from a string where the tokens are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.