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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:41:12+00:00 2026-05-30T08:41:12+00:00

I have multiple foreach loops through which i am fetching the work history ,

  • 0

I have multiple foreach loops through which i am fetching the work history , education history of the users from Facebook and inserting these into a database:

Result soap = serializer.Deserialize<Result>(ser);
foreach (var item in soap.data)
{
    int length = item.education_history.Length;
    int lenght2 = item.work_history.Length;

    foreach (var edu in item.education_history)
    {
        length--; 
        ftr = ftr + "," + edu.name.ToString();
    }

    foreach (var wrk in item.work_history)
    {
        lenght2--;
        ftr1 = ftr1 + "," + wrk.company_name.ToString();
    }

    string str1 = "Insert into [Snaps] (Loc_city,Loc_state,Loc_country,Edu_Hist1,Work_Hist1) values ('" + item.current_location.city.ToString() + "','" + item.current_location.state.ToString() + "','" + item.current_location.country.ToString() + "','" + ftr + "','" + ftr1 + "')";
    SqlCommand cmd1 = new SqlCommand(str1, con);
    cmd1.ExecuteNonQuery();
}

PS: This code is just for testing purposes thus not used parameterized sql query

The problem i am encountering is that when i am calculating the values of ftr and ftr1 of the first item in the parent foreach loop

foreach (var item in soap.data)

and when i insert these values into the database it appends the second array index in item and saves it in the database. Also the values of ftr and ftr1 are not getting refreshed it appends the new values with the previous ones

What am i doing wrong here?

Thanks

  • 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-05-30T08:41:13+00:00Added an answer on May 30, 2026 at 8:41 am

    The problem is that the ftr and ftr1 variables are not being reset after each iteration in the main loop, hence the values keep being concatenated to them.

    One solution would be to set both ftr and ftr1 to null or empty strings at the end of the main loop:

    foreach (var item in soap.data)
    {
        foreach (var edu in item.education_history)
        {
            // ...
        }
    
        foreach (var wrk in item.work_history)
        {
            // ...
        }
    
        // ...
    
        ftr = null;
        ftr1 = null;
    }
    

    However, a simpler approach would be to use the Enumerable.Select method in LINQ to extract the lists of values from the education_history and work_history arrays and use String.Join to concatenate them into two strings:

    foreach (var item in soap.data)
    {
        var educationNames = item.education_history.Select(i => i.name.ToString());
        var ftr = String.Join(",", educationNames);
    
        var previousCompaniesNames = item.work_history.Select(i => i.company_name.ToString());
        var ftr1 = String.Join(",", previousCompaniesNames);
    
        var statement = "INSERT INTO [Snaps] (Loc_city,Loc_state,Loc_country,Edu_Hist1,Work_Hist1) VALUES (@city, @state, @country, @educationNames, @previousCompaniesNames)";
        var command = new SqlCommand(statement, con);
        command.Parameters.Add("@city", SqlDbType.VarChar, 255).Value = item.current_location.city.ToString();
        command.Parameters.Add("@state", SqlDbType.VarChar, 255).Value = item.current_location.state.ToString();
        command.Parameters.Add("@country", SqlDbType.VarChar, 255).Value = item.current_location.country.ToString();
        command.Parameters.Add("@educationNames", SqlDbType.VarChar, 4000).Value = ftr
        command.Parameters.Add("@previousCompaniesNames", SqlDbType.VarChar, 4000).Value = ftr1
        command.ExecuteNonQuery();    
    }
    

    This way the ftr and ftr1 variables are declared at each iteration, so there’s no need to manually reset their value.

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

Sidebar

Related Questions

I have an aprox 1000 line-script which loops through all servers in AD. The
We have multiple MFC apps, which use CMutex( false, blah ), where blah allows
I have multiple classes that all derive from a base class, now some of
I use a foreach loop to loop through multiple seed urls. During each loop,
I have multiple select statements from different tables on the same database. I was
I have a GridView which displays in excess of 30000 rows, and users need
I have a form, which contains multiple virtual fieldsets. Eg, MyForm: Name1 Age1 Location1
I have multiple forms on a page, for each of them I want the
There are multiple values I have been storing in ASP.NET configSections sections for each
I have a program that works with multiple threads. For each I set uncaught

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.