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 8130227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:38:06+00:00 2026-06-06T08:38:06+00:00

I have two datatables and I need to match zipcodes from each of the

  • 0

I have two datatables and I need to match zipcodes from each of the datatables, and every time the program finds a match, I need to set a column in the matched rows to a string.
Here is my code.

I do not know what to do in the foreach loop, or if there is an easier way to do this, please say it.

        //Datatable for entire list of zipcodes
        DataTable datat = new DataTable("DATA");
        DataColumn state = new DataColumn();
        DataColumn county = new DataColumn();
        DataColumn zipcode = new DataColumn();
        DataColumn latitude = new DataColumn();
        DataColumn longitude = new DataColumn();
        DataColumn salesperson = new DataColumn();
        DataColumn originalrec = new DataColumn();

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

        return datat;
    }

    private static DataTable InitData2()
    {
        //Datatable for entire list of zipcodes
        DataTable datat2 = new DataTable("DATA");
        DataColumn ctype = new DataColumn();
        DataColumn csalesperson = new DataColumn();
        DataColumn czipcode = new DataColumn();

        //Add the columns to the datatable
        datat2.Columns.Add(ctype);
        datat2.Columns.Add(csalesperson);
        datat2.Columns.Add(czipcode);

        return datat2;
    }       

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

    private static String InitPath2()
    {
        string path2 = "C:/Documents and Settings/Andre/Desktop/SalesMap/CUSTOMER_PROSPECT_SALESPERSON.csv";
        return path2;
    }

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

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

                Console.WriteLine("Row added for: dt");
            }
            dt.WriteXml(@"c:\test\dt1.xml");
            sr.Close();

            while ((csvData2 = sr2.ReadLine()) != null)
            {
                String[] data2 = csvData2.Split(',');
                DataRow newRow2 = dt2.NewRow();
                newRow2[0] = data2[0].ToString();
                newRow2[1] = data2[1].ToString();
                newRow2[2] = data2[2].ToString();
                dt2.Rows.Add(newRow2);

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

            dt2.WriteXml(@"c:\test\dt2.xml");
            sr2.Close();

            foreach (DataRow row1 in dt.Rows)
            {
                //Dont know what to do here
            }
        }

        catch (Exception e)
        {
            //Console.WriteLine("The files could not be read:");
            //Console.WriteLine(e.Message);
            StreamWriter sw = new StreamWriter(@"c:\test\error.txt");
            sw.WriteLine(e.Message);
            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-06T08:38:07+00:00Added an answer on June 6, 2026 at 8:38 am

    Not sure if i understood at all. But as i can imagine the look it should be like this:

                DataTable dtSampleOne = new DataTable();
                DataTable dtSampleTwo = new DataTable();
    
                foreach (DataRow rowSampleOne in dtSampleOne.Rows)
                {
                    string zipCodeSampleOne = rowSampleOne["zipCodeToMatchOne"].ToString();
    
                    foreach (DataRow rowSampleTwo in dtSampleTwo.Rows)
                    {
                        if (rowSampleTwo["zipCodeToMatchTwo"].ToString().Equals(zipCodeSampleOne))
                        {
                            // Do your stuff here
                        }                            
                    }
                }
    

    I’ll keep in touch if you need anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two DataTables, A and B , produced from CSV files. I need
I have two datatables. Each has three columns, the two of them have the
I need to marge two datatables with condition. I have a datatable where the
I have two datasets each with one data table pulled from different sources and
I have a C# program that selects data from two different database files and
I have a bunch of XSD's from https://github.com/XeroAPI/XeroAPI-Schemas/tree/master/v2.00 I need to do two things:
I need to compare numeric data from two datatables with the same schema. For
I have a dropdown. I need to bind with two field of datatable value
I have a task where I need to translate a DataTable to a two-dimensional
I have two datatables. One is to display names. Another contains three <h:selectOneMenu> .

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.