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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:33:25+00:00 2026-06-05T06:33:25+00:00

Below you see the code for my calender for a website, the problem is

  • 0

Below you see the code for my calender for a website, the problem is that this code takes about 8-10 sec to load. If any of you guys can see a way to minimize the load time I would be grateful.

    public static IEnumerable<DateTime> AllDatesInMonth(int year, int month)
    {
        foreach (var day in Enumerable.Range(1, DateTime.DaysInMonth(year, month)))
        {
            yield return new DateTime(year, month, day);
        }
    }

    public void ForeachDayInMonth(int year, int month, SqlConnection connection)
    {
        int day;
        int count;
        double divHeight;

        lbl_Month.Text += "<table class=\"Month\">";
        foreach (DateTime date in AllDatesInMonth(year, month))
        {
            day = int.Parse(date.ToString().Substring(0, 2));
            count = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection).Count; // Creates a sql select statement

            lbl_Month.Text += "<tr>" +
                "<td style=\"height: 30px; width: 70px;";

            if (date.Date == DateTime.Today)
            {
                lbl_Month.Text += "-webkit-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" +
                    "-moz-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" +
                    "box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);";
            }

            lbl_Month.Text += "\">";

            if (count != 0)
            {
                divHeight = 100 / count;

                foreach (Begivenheder b in Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection)) // creates a sql select statement
                {
                    lbl_Month.Text += "<div style=\"background-color: " + b.begivenhed.type.TypeFarve + "; height: " + divHeight + "%;\">" +
                        "<a href=\"KalenderEvent.aspx?Event=" + b.begivenhed.ID + "\">";
                    if (b.begivenhed.Navn.Length > 9)
                    {
                        lbl_Month.Text += b.begivenhed.Navn.Remove(9) + "...";
                    }
                    else
                    {
                        lbl_Month.Text += b.begivenhed.Navn;
                    }
                    lbl_Month.Text += "</a>" +
                        "</div>";
                }
            }

            lbl_Month.Text += "</td>" +
                "</tr>";
        }
        lbl_Month.Text += "</table>";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string year;

        if (Request.QueryString["Year"] == null)
        {
            year = DateTime.Today.Year.ToString();
        }
        else
        {
            year = Request.QueryString["Year"];
        }

        Page.Title += " - Kalender " + year;

        SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]);
        connection.Open();
        try
        {
            Year(int.Parse(year), connection);
        }
        finally
        {
            connection.Close();
        }
    }

    private void Year(int year, SqlConnection connection)
    {
        string thisYear = year.ToString();
        int lastYear = year - 1;
        int nextYear = year + 1;

        lbl_Year.Text = "<div>" +
            "<a href=\"Kalender.aspx?Year=" + lastYear.ToString() + "\" id=\"LastYear\"></a>" +
            "</div>" +
            "<div>" +
            thisYear +
            "</div>" +
            "<div>" +
            "<a href=\"Kalender.aspx?Year=" + nextYear.ToString() + "\" id=\"NextYear\"></a>" +
            "</div>";

        lbl_Month.Text = "<table>" +
            "<tr>" +
            "<td>";
        // Dage
        lbl_Month.Text += "<p>" +
            "</p>" +
            "<br />" +
            "<table>";
        for (int i = 1; i <= 31; i++)
        {
            lbl_Month.Text += "<tr>" +
                "<td style=\"height: 30px;\">" +
                "<p>" +
                i.ToString() +
                "</p>" +
                "</td>" +
                "</tr>";
        }
        lbl_Month.Text += "</table>" +
            "</td>";

        //Januar (repeats 12 times, ones for each month)
        lbl_Month.Text += "<td>" +
            "<p>" +
            "Januar" +
            "</p>";
        ForeachDayInMonth(int.Parse(thisYear), 1, connection);
        lbl_Month.Text += "</td>";

        lbl_Month.Text += "</tr>" +
            "</table>";
    }

heres the “Begivenheder.Get_Begivenhed_By_Date” code.

    public static List<Begivenheder> Get_Begivenhed_By_Date(int år, int måned, int dag, SqlConnection connection)
    {
        List<Begivenheder> result = new List<Begivenheder>();

        using (var command = new SqlCommand("Select ID, FK_Begivenhed_ID from Begivenhed_Datoer where Dag=" + dag + " and Måned=" + måned + " and År=" + år, connection))
        {
            SqlDataReader reader = command.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    Begivenheder b = new Begivenheder();
                    b.Dato_ID = reader.GetInt32(0);
                    b.ID = reader.GetInt32(1);
                    b.Dato_Day = dag;
                    b.Dato_Month = måned;
                    b.Dato_Year = år;
                    result.Add(b);
                }
            }
            finally
            {
                reader.Close();
            }

            foreach (Begivenheder b in result)
            {
                b.begivenhed = Begivenheder.Get_Begivenhed_By_ID(b.ID, connection);
            }
        }

        return result;
    }

I know it’s a bunch of code, but I have no idea how to decrease it.

  • 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-05T06:33:26+00:00Added an answer on June 5, 2026 at 6:33 am

    Every page_load leads to cca 30 database queries – Thats a lot of overhead, try and revamp it so you only need to talk to the database once, if at all. Besides that, get a .NET profiler and see where you spend most time.

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

Sidebar

Related Questions

See code below: I want Page 1 to alert #testing... This works in C
I've a problem with CakePHP __() function. Please see the code below : $options
I am having a problem using the signedCMS.decode routine. See the code below. The
I can't get the code below to compile (see errors). Advice on correction would
I have some really funky code. As you can see from the code below
I can't see where I'm going wrong with the code below, its probably something
Can anyone help me understand why my code (see below) is resulting in the
I have a problem with some jquery please see below code. I have 2
This might be a little complicated. See code below. When image is clicked I
I see below code in this link : An elegant way to implement INotifyPropertyChanged

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.