My For Statement Is Displaying My HTML Page Multiple Times Instead Of Just Once

I am trying to go through an HTML page and replace each numbered CampaignID and add a div tag with a number and z-index to highlight each link. The for statement is replacing each div tag with the next CampaignID 1-10 but it is printing out 10 of the same HTML page with each having the next numbered CampaignId and div tag replaced. I just need one page with each "a href" numbered on the page. What do I need to change in this code to make that happen? I would put the output for you to see but it is too much HTML code.

using (SqlConnection connection =
                       new SqlConnection(con))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                
                string CampaignId = reader["CampaignId"].ToString();
                string htmlCreate= reader["ConvertedCreative"].ToString();
                StringBuilder strContents = new StringBuilder();
                for(int i = 1; i <= 10; i++)
                {

                    string replacement = "<div style=\"z-indexer:1;background:#0c538e;width:2.25em;height:2.25em;text-align:center;color:white;\">" + i + "</div><a href=\"http://www.trackingstats.info/Process.aspx.c=/" + CampaignId + "&l=" + i + "\"";
                    Regex regex = new Regex("<a href=\"http://www.trackingstats.info/Process.aspx.c=24896&l=" + i + "\"", RegexOptions.IgnoreCase);
                    string result = regex.Replace(htmlCreate, replacement);
                    strContents.AppendLine(result);





                }
                Label1.Text = strContents.ToString();
            }
           
        }

here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

If I’m understanding your issue correctly, it sounds like you need to move the line strContents.AppendLine(result); outside of your for loop. You are adding the result text to the StringBuilder each time through the loop, which would equate to 10 times.

In fact, if you don’t want to duplicate it 10 times, you shouldn’t need the StringBuilder.

Perhaps something like this?

for(int i = 1; i <= 10; i++)
{
    string replacement = "<div style=\"z-indexer:1;background:#0c538e;width:2.25em;height:2.25em;text-align:center;color:white;\">" + i + "</div><a href=\"http://www.trackingstats.info/Process.aspx.c=/" + CampaignId + "&l=" + i + "\"";
    Regex regex = new Regex("<a href=\"http://www.trackingstats.info/Process.aspx.c=24896&l=" + i + "\"", RegexOptions.IgnoreCase);
    htmlCreate = regex.Replace(htmlCreate, replacement);
}
Label1.Text = htmlCreate;

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply