C. Fuller
C. Fuller

Reputation: 44

Why are using multiple foreach loops in single page resulting in first foreach duplicating with c#?

I am trying to pull data from two separate tables in a database. The first tables contains information about services and the second table contains information about the company. I have created the class page for each list and I have also updated my connection strings to include the relevant information. Here is what I have tried so far and the results.

If I comment out only the second foreach, only the first one renders on the website - and it renders perfectly.

If I comment out only the first foreach, only the second one renders on the website - and it renders perfeclty.

If neither are commented out, the first one renders, as it should, on the website. It then repeats, taking on the formatting of the second foreach, thus duplicating the foreach.

I have tried using break(); and got an error message. I also tried using return();

I am writing this in VisualStudio 2017, c#.

private void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
    con.Open();
    str = "select * from Services where Id != 0";
    com = new SqlCommand(str, con);
    SqlDataReader reader = com.ExecuteReader();
    ArrayList servicesList = ConnectionClass.GetServicesbyId(lblTitle.Text);
    StringBuilder sb = new StringBuilder();

    foreach (Services services in servicesList)
    {
        sb.Append(string.Format(@"<div class='col-md-2 col-xs-4'><a href='#'><img class='image-responsive' src='{0}' alt='{1}' /></a><a href='#'><h2>{1}</h2></a><p>{2}</p><a href='#' class='btn btn-lg btn-default btn-block' id='{3}'>{1}</a></div>",
           services.service_box_image, services.title, services.service_short_description, services.services_id));

        lblTitle.Text = sb.ToString();
    }

    reader.Close();
    con.Close();

    str = "select * from Company_Info where Id != 0";
    com = new SqlCommand(str, con);
    ArrayList companyList = ConnectionClass.GetCompanyInfo(lblTagline.Text);

    foreach (Company company in companyList)
    {
        sb.Append(string.Format(@"<div class='col-md-7'><h1>{0}</h1><p class='lead'>{1}</p>
        </div><div class='col-md-1'></ div>",
           company.CompanyTaglineShort, company.CompanyTaglineLong, company.CompanyPhoneMain));

        lblTagline.Text = sb.ToString();

    }

    reader.Close();
    con.Close();
}

Upvotes: 0

Views: 127

Answers (1)

Steve
Steve

Reputation: 216333

Calling ToString on the StringBuilder variable doesn't remove the content of the StringBuilder. The data is still there, then when you continue to add to the same variable and call StringBuilder.ToString method again you get also the data added in the previuous loop.

You simply need to clear the content of the variable between the two loops. You should call StringBuilder.Clear or just set the StringBuilder.Length = 0;

foreach (Services services in servicesList)
{
   .....
}
....
sb.Clear(); 
....
foreach (Company company in companyList)
{
   ....
}

Also, as noted below from Hans Kesting, you should probably write your labels just one time outside the loops

foreach (Services services in servicesList)
{
   .....
}
lblTitle.Text = sb.ToString()
sb.Clear();
....
foreach (Company company in companyList)
{
   ....
}
lblTagline.Text = sb.ToString();

Upvotes: 2

Related Questions