Consuming the Holidaywebservice2 with Visual Studio 2010





5.00/5 (1 vote)
How to use the excellent Holiday web service in VB.NET using VS 2010.
The documentation provided on the web service site[^] is patchy to say the least, especially for ASP.NET. It only directs you to the generic MSDN web service walkthrough page. After a few fruitless hours of searching the internet for the best way to use the new HolidayWebService2[^], I managed to solve it myself.
For my purposes, marking a public holiday on a calendar object on a web form, I found this to be the best method. In this example, I am loading a local array from the published one for all the holidays in the current year.
- Add the web service in VS 2010 by pasting http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx[^] into the web service dialog.
- In your code behind page, add a one dimensional array to hold the data and an object for the service in your page declarations. Note the use of the web service to define the array type.
- In your
Page_Load
event, add the code to load the data from the web service. - Create the
GetHolidays
procedure for the current year. The first parameter defines the country code, in this case 1 = GreatBritain. - Finally you can use the array in the day render code of the calendar control to put a border round the holiday day and set a tool tip to display its name, which is held in the
Descriptor
element.BankHoliday = 0
means that it is a 'Recognized' public holiday, 1 is used for notable dates such as Valentine's day, which is not a holiday.
Public Class CalendarPage
Inherits System.Web.UI.Page
Public arrHolidays() As com.holidaywebservice.www.Holiday
Dim wsHolidays As New com.holidaywebservice.www.HolidayService2
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
calCalendar.VisibleDate = _
DateSerial(Year(DateTime.Today), Month(DateTime.Today), 1)
GetHolidays(Year(DateTime.Today))
.
Private Sub GetHolidays(ByVal intYr As Integer)
arrHolidays = wsHolidays.GetHolidaysForYear(1, intYr)
End Sub
Private Sub calCalendar_DayRender(ByVal sender As Object, _
ByVal e As WebControls.DayRenderEventArgs) Handles calCalendar.DayRender
Dim nextDate As DateTime
e.Cell.BorderWidth = 3
e.Cell.BorderColor = Drawing.Color.White
For Each objHoliday As com.holidaywebservice.www.Holiday In arrHolidays
If objHoliday.Date = e.Day.Date And objHoliday.BankHoliday = 0 Then
e.Cell.BorderColor = Drawing.Color.Black
e.Cell.ToolTip = String.Concat(e.Cell.ToolTip, " ", objHoliday.Descriptor)
End If
Next
End Sub
I hope this proves useful to anyone else using this web service.