A group blog from members of the VB team
Microsoft WebMatrix is an exciting new product that makes the creation of custom ASP.NET Web sites possible without all the complexity of the full Visual Studio environment. WebMatrix supports web site development in both C# and Visual Basic. In this blog post, I will demonstrate how to create VB websites using Webmatrix.
Before you begin, you need to have the following settings configured on your machine:
You can create the Visual Basic websites using WebMatrix in the four simple steps as follows:
Step 1: Select a Template Step 2: Create a Website using the Calendar Visual Basic Template Step 3: Edit and Modify the Website Step 4: Understand and Customize the Code
You can create a website using any of the templates that are available in WebMatrix, as per your preference. In this step, we will see which all different templates are available that can be used to create a new website.
In this step, we will see how to create a website from the selected Visual Basic template. Once the Calendar VB template is selected, a default website is created based on the Calendar VB template. WebMatrix displays it in the Site workspace.
To view how your website will look on the Internet, click “Run”. The website opens in the default browser.
You have created the website using the VB template. Now you can, modify and customize the site as per your preference. In this step, we will see how to modify the website.
In this step, we will see how to modify and customize some part of the code in order to add a couple of validations.
By default, when we try to create a calendar with a color that has already been used, the template still allows us to create the new calendar for the used color. To avoid this let us modify the code to allow validation of the calendar color before creating a new calendar.
The color to check ''' whether color found or notPublic Shared Function IsColorAvailable(ByVal userId As Integer, ByVal Color As String) As Object Dim db = UserHelper.DatabaseInstance Return db.QuerySingle("SELECT * from CalendarsUsers " & "WHERE UserId = @0 " & "AND color = @1 ", userId, Color) End Function
If NOT ColorHelper.IsColorAvailable(WebSecurity.CurrentUserId, _color) Is Nothing Then ModelState.AddError("color", "This color is already in use.") End If
By default, when we try to create a calendar event, the template allows us to select an end time of calendar event smaller than the start time, which is logically incorrect. To remove this let us modify the code to allow validation of the event start time and end time before creating a new calendar event.
' Validate the start and end date/times.
If (Not Date.TryParse(Page.StartDate & " " & Page.StartTime, start)) OrElse (Not Date.TryParse(Page.StartDate & " " & Page.EndTime, [end])) Then ModelState.AddFormError("One of the date / time strings were unreadable.") End If
If start > [end] Then ModelState.AddFormError("End Time should be later than Start Time") End If
The “Calendar” folder contains a set of pages that are used to perform various functionalities such as adding a calendar, changing the theme of a calendar, changing the view of the calendar, deleting and editing the calendar, and to display the information of the calendar. The following methods are created in the Calendar class of the Calendar.vb page in the “App_Code” folder. Review the following code for different methods:
Public Shared Sub UpdateCalendarColor(ByVal userId As Integer, ByVal calendarId As Integer, ByVal color As String) Dim db = UserHelper.DatabaseInstance db.Execute("UPDATE CalendarsUsers SET Color = @0 WHERE CalendarId = @1 AND UserId = @2", color, calendarId, userId) End Sub
Public Shared Sub UpdateCalendar(ByVal userId As Integer, ByVal calendarId As Integer, Optional ByVal calendarName As String = Nothing, Optional ByVal color As String = Nothing) Dim db = UserHelper.DatabaseInstance color = If(color, ColorHelper.GetRandomColor()) calendarName = If(calendarName, "Default")
' We are editing a calendar db.Execute("UPDATE Calendars SET Name = @0 WHERE Id = @1", calendarName, calendarId) UpdateCalendarColor(userId, calendarId, color) End Sub
Public Shared Function CreateCalendar(ByVal userId As Integer, Optional ByVal calendarName As String = Nothing, Optional ByVal color As String = Nothing) As Integer Dim db = UserHelper.DatabaseInstance color = If(color, ColorHelper.GetRandomColor()) calendarName = If(calendarName, "Default")
' Create a new calendar db.Execute("INSERT INTO Calendars (Name, Creator, Guid) Values (@0, @1, @2)", calendarName, userId, GenerateUniqueId()) Dim calendarId = Convert.ToInt32(db.GetLastInsertId())
' Add the calendar id to CalendarUsers db.Execute("INSERT INTO CalendarsUsers (CalendarId, UserId, Permissions, Color) VALUES (@0, @1, @2, @3)", calendarId, userId, Permission.Own, color) Return calendarId End Function
Public Shared Sub DeleteCalendar(ByVal calendarId As Integer) Dim db = UserHelper.DatabaseInstance db.Execute("DELETE FROM Events WHERE CalendarId = @0", calendarId) db.Execute("DELETE FROM CalendarsUsers WHERE CalendarId = @0", calendarId) db.Execute("DELETE FROM Calendars WHERE Id = @0", calendarId) End Sub
Public Shared Function GetUserCalendar(ByVal userId As Integer, ByVal calendarId As Integer) As Object Dim db = UserHelper.DatabaseInstance Return db.QuerySingle("SELECT c.Name, c.Creator, c.Guid, cu.* " & "FROM Calendars AS c " & "JOIN CalendarsUsers AS cu ON c.Id = cu.CalendarId " & "WHERE cu.UserId = @0 " & "AND cu.CalendarId = @1 ", userId, calendarId) End Function
Public Shared Function GetCalendarGroups(ByVal userId As Integer) As Object ' I should maintain a cache of all of the calendars loaded on the page ' to reduce calendar queries / joins Dim calendars = Calendar.GetUserCalendars(userId) Dim calendarGroups = ( From c In CType(calendars, IEnumerable(Of Object)) Group c By GroupKey = c.Permissions >= CInt(Permission.Own) Into g = Group Order By GroupKey Select New With { Key .Own = If(GroupKey, "Mine", "Shared"), Key .Calendars = g.OrderBy(Function(c) c.Name)}).ToDictionary(Function(g) g.Own, Function(g) g.Calendars) Return calendarGroups End Function
Public Shared Function GetCalendar(ByVal calendarId As Integer) As Object Dim db = UserHelper.DatabaseInstance Return db.QuerySingle("SELECT * FROM Calendars WHERE Id = @0", calendarId) End Function
Public Shared Function GetCalendarByHash(ByVal calendarId As Integer, ByVal hash As String) As Object Dim db = UserHelper.DatabaseInstance Return db.QuerySingle("SELECT * FROM Calendars WHERE Id = @0 AND Guid = @1", calendarId, hash) End Function
The “Event” folder contains various pages that are used to perform various functionalities such as adding a calendar event, deleting a calendar event, downloading the calendar event in “.ics” format, editing a calendar event, and to display the information of a calendar event. The following methods are created in the Event class of the Event.vb page in the “App_Code” folder. Review the following code for different methods:
Public Shared Function AddEvent(ByVal userId As Integer, ByVal calendarId As Integer, ByVal name As String, ByVal description As String, ByVal location As String, ByVal utcStart As Date, ByVal utcEnd As Date, Optional ByVal allDay As Boolean = False) As Integer Dim db = UserHelper.DatabaseInstance db.Query("INSERT INTO Events " & "(OrganizerId, CalendarId, Name, Description, Location, AllDay, Start, [End]) " & "Values (@0, @1, @2, @3, @4, @5, @6, @7) ", userId, calendarId, name, description, location, allDay, utcStart, utcEnd) Return Convert.ToInt32(db.GetLastInsertId()) End Function
Public Shared Sub DeleteEvent(ByVal eventId As Integer) Dim db = UserHelper.DatabaseInstance db.Execute("DELETE FROM EVENTS WHERE Id = @0", eventId) End Sub
Public Shared Function GetUserEvent(ByVal userId As Integer, ByVal eventId As Integer) As Object Dim db = UserHelper.DatabaseInstance Return db.QuerySingle( "SELECT e.*, c.Name AS CalendarName, c.Id AS CalendarId, cu.Color, cu.Permissions " & "FROM EVENTS AS e " & "JOIN Calendars AS c ON e.CalendarID = c.Id " & "JOIN CalendarsUsers AS cu ON c.Id = cu.CalendarId " & "WHERE cu.UserId = @0 AND e.Id = @1 ", userId, eventId) End Function
Public Shared Function GetCalendarEvents(ByVal calendarId As Integer) As Object Dim db = UserHelper.DatabaseInstance Return db.Query("SELECT * FROM Events WHERE CalendarId = @0", calendarId) End Function
The “Themes” folder contains various default templates themes for the calendar. These template themes contain different predefined layouts, sections, and styles. The user can select any of the following themes to be applied it to their calendars:
That’s it! You have now successfully created your first website based on the Visual Basic templates using Microsoft WebMatrix. In our next post, we will see how to publish the website for the others to see.
i need form design and coding pls help me