Tutorial: Event Calendar Printing (ASP.NET, C#, VB.NET, SQL Server)

This tutorial shows how to print the event calendar using the built-in PNG export feature. Sample Visual Studio with C# and VB.NET source code available for download.
Mar 26, 2014
event calendar printing asp.net

Tutorial:

Features:

  • Editable weekly event calendar
  • Special print page with custom layout, with the event calendar embedded as PNG image
  • Visual Studio 2010 solution
  • SQL Server database
  • C# web project
  • VB.NET web project

Sample source code of the Png.aspx file that returns the calendar image PNG file (C#):

public partial class _Png : System.Web.UI.Page 
{
  DayPilotCalendar DayPilotCalendar1 = new DayPilotCalendar();

  protected void Page_Load(object sender, EventArgs e)
  {
      DayPilotCalendar1.ViewType = ViewTypeEnum.Week;
      DayPilotCalendar1.StartDate = Convert.ToDateTime(Request.QueryString["start"]);

      SetDataSourceAndBind();
      ExportToPng();
  }


  private void SetDataSourceAndBind()
  {
      DayPilotCalendar1.DataSource = GetData(DayPilotCalendar1.StartDate, DayPilotCalendar1.EndDate);
      DayPilotCalendar1.DataStartField = "eventstart";
      DayPilotCalendar1.DataEndField = "eventend";
      DayPilotCalendar1.DataIdField = "id";
      DayPilotCalendar1.DataTextField = "name";
      DayPilotCalendar1.DataBind();

  }

  private DataTable GetData(DateTime start, DateTime end)
  {
      SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [event] WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
      da.SelectCommand.Parameters.AddWithValue("start", start);
      da.SelectCommand.Parameters.AddWithValue("end", end);

      DataTable dt = new DataTable();
      da.Fill(dt);

      return dt;
  }



  private void ExportToPng()
  {
      SetDataSourceAndBind();
      SetExportProperties();

      int scroll = Convert.ToInt32(Request.QueryString["scroll"]);

      Response.Clear();
      Response.ContentType = "image/png";
      MemoryStream img = DayPilotCalendar1.Export(ImageFormat.Png, scroll);
      img.WriteTo(Response.OutputStream);
      Response.End();
  }



  private void SetExportProperties()
  {
      // match the theme
      
      DayPilotCalendar1.HourNameBackColor = ColorTranslator.FromHtml("#eee");
      DayPilotCalendar1.BackColor = Color.White;
      DayPilotCalendar1.NonBusinessBackColor = Color.White;
      DayPilotCalendar1.BorderColor = ColorTranslator.FromHtml("#999");
      DayPilotCalendar1.HeaderFontColor = ColorTranslator.FromHtml("#666");
      DayPilotCalendar1.CellBorderColor = ColorTranslator.FromHtml("#eee");
      DayPilotCalendar1.EventFontColor = ColorTranslator.FromHtml("#666");
      DayPilotCalendar1.EventCorners = CornerShape.Rounded;
      DayPilotCalendar1.EventFontSize = "10pt";
      DayPilotCalendar1.EventBorderColor = ColorTranslator.FromHtml("#999");
      DayPilotCalendar1.EventBackColor = ColorTranslator.FromHtml("#fafafa");
      DayPilotCalendar1.HourBorderColor = ColorTranslator.FromHtml("#eee");
      DayPilotCalendar1.HourHalfBorderColor = ColorTranslator.FromHtml("#eee");
      DayPilotCalendar1.DurationBarVisible = false;

  }

The full source code of the sample application is available for download.