Home
        Wiley ASP.NET 2.0 Instant Results
         Contents
1.    The Online Diary  and Organizer    By the end of this chapter you ll have created an online diary  organizer  and contacts manager  So  what exactly does the online diary and organizer do  Using a calendar based interface it allows  you to add  delete  and edit a diary entry for any day  It also allows you to create events  for exam   ple  to keep a note of your rich Uncle Bob   s birthday     wouldn   t want to forget that  would you   It   s not just limited to birthdays  but any event  meetings  appointments  and so on     The system has a basic username and password logon system  so that only you and no one else  can view your own diary  This is what differentiates it from a blog  This system is a private diary  and contacts manager     a place to put all those thoughts and comments you d rather not have the  world see  Unlike a blog  where you want the world to see it     This whole project demonstrates the power of ASP NET 2 0 and how easy it makes creating pro   jects like this  Gone are the days of hundreds of lines of code to do security logons  create new  users  and so on  This chapter employs the new security components of ASP NET 2 0 to show just  how easy it is to create fun  exciting  and useful projects     The first section takes you through using the diary and its main screens  Then  the    Design of the  Online Diary    section walks you through an overview of the system   s design  After that you get  into the nuts and bolts of the system and how it all
2.    lt HeaderStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White    gt    lt EditRowStyle BackColor   999999    gt    lt AlternatingRowStyle BackColor  White  ForeColor   284775    gt    lt  asp GridView gt        Again  the AutoGenerateColumns parameter is set to False  and the columns are specified as follows      lt Columns gt    lt asp HyperLinkField DataNavigateUrlFields  EventId  Text  Edit   DataNavigateUrlFormatString    SecureDiary EditEvent aspx EventId  0     gt    lt asp CommandField ShowDeleteButton  True    gt    lt asp BoundField DataField  EventName  HeaderText  Event    gt    lt asp BoundField DataField  EventDescription   HeaderText  Description    gt    lt  Columns gt     Notice the hyperlink and field that when clicked will take the user to the EditEvent aspx page  and the    URL contains data passed to the Event Id in the URL by way of the Event Id querystring parameter  It   s set  to be  0   which at run time will be substituted by the value of the first column for each row in the DataSet     30    The Online Diary and Organizer    In addition  the code specifies a Delete button on each row in the grid    lt asp CommandField ShowDeleteButton  True    gt     When you click the Delete button  the GridView control asks the ObjectDataSource control to call the  specified delete method of the data providing class  In this case it   s the DeleteEvent    method of the  DiaryEvent class  The DataKkeyNames attribute in the GridView control   s markup spec
3.   The DiaryEventCollection class inherits from the System  Collections CollectionBase class  Its  purpose is simply to store a collection of DiaryEvent objects  The class employs the following methods     Method    Add  ByVal NewDiaryEvent As    DiaryEvent        Add  ByVal DiaryEventId  As Long     Remove  ByVal Index As  Integer     16    Return Type Description    None    None    None    Adds a DiaryEvent object to the collection  held by the DiaryEventCollection  object        Creates a new DiaryEvent object   DiaryEventId is passed to the  DiaryEvent object   s constructor to ensure  it   s populated with the event   s details from  the database  The new DiaryEvent object  is then added to the collection maintained  by the DiaryEventCollection object        Removes the DiaryEvent object from the  collection at the specified index     The Online Diary and Organizer  This class contains only one property     Property Type Description  Item ByVal Index As Integer  Integer Returns the DiaryEvent object stored at    the position in index in the collection     That completes an overview of all the classes and their design  methods  and properties  The next section  takes a more in depth look at the code and the  aspx pages dealing with presentation     Code and Code Explanation    This section digs into each of the important pages and shows you how they interact with each other  as  well as how they use the classes in the business layer  This section doesn   t cover every sing
4.  1 2  challenge  Gripped the remains of the steering column tightly 24 8 s 1 W    and made it home safely  wou 12 13 14 1s  iz 18  18 20 21 22 23  24 25 26 27 28 20  a1 i 2 3 6  Save Entry    Add New Event  Event Description  Edit Delete Purchase Steering Wheel Buy a new steering wheel for the car  Edit Delete Ring Jill Call Jill about contract  Figure 1 5    Chapter 1    You can also navigate your diary from here via the small calendar to the right     Adding a diary entry simply involves typing in the Entry Title and Diary Text boxes and clicking the  Save Entry button     Events happening on a particular day are listed in the Events table at the bottom left of Figure 1 5  You  can edit and delete events  or click the Add New Event link to add a new event  The Edit and Add event  pages are almost identical in look  An example of the Edit Event page is shown in Figure 1 6        22 October 2005    Edit Event    Event Name   Buy shopping    Event Description     Buy loaf of bread    Start Time   i4 z  fio       Event Duration  minutes   30       Save Event               Figure 1 6    In the Edit Event page  you can set the event   s name  include a brief description  what time the event  starts  and how long it lasts     Returning to the main diary page  refer to Figure 1 4  you ll see a Manage Your Contacts link  as shown  in Figure 1 7        You Are Logged On As  juserS       Manage Your Contacts             Upcoming Events     Event Date Event  Nov  22 10 2005 ees  14 10 
5.  directory of the diary project     The markup for the Login control is shown here         lt asp Login ID  Loginil  runat  server  BackColor   F7F6F3  BorderColor   E6E2D8   BorderPadding  4    BorderStyle  Solid  BorderWidth  1px  CreateUserText  Not  registered  Click here to register now     CreateUserUrl    RegisterStart aspx   DestinationPageUrl    SecureDiary DiaryMain aspx  Font Names  Verdana    Font Size  0 8em  ForeColor   333333  Height  197px   PasswordRecoveryText  Forgotten your password     PasswordRecoveryUrl    PasswordReminder aspx  Style  z   index  100  left  78px    position  absolute  top  55px  Width  315px  gt     lt LoginButtonStyle BackColor   FFFBFF  BorderColor   CCCCCC   BorderStyle  Solid  BorderWidth  1px     18    The Online Diary and Organizer    Font Names  Verdana  Font Size  0 8em   ForeColor   284775    gt    lt TextBoxStyle Font Size  0 8em    gt    lt TitleTextStyle BackColor   5D7B9D  Font Bold  True  Font   Size  0 9em  ForeColor  White    gt    lt InstructionTextStyle Font Italic  True  ForeColor  Black          gt    lt  asp Login gt     Important attributes to note are Dest inationPageUr1  which determines where the user is navigated to  if he or she enters a valid username and password  In the Online Diary project it   s the Diarymain aspx  page  the center of the Online Diary   s interface     To enable new users to register  the CreateUserText has been set to a friendly    register here    message   the URL for registering is sp
6.  hangs together  In the final section  you set up  the diary     Using the Online Diary    Each user has his or her own online diary  to access it requires logging on  Enter username user5  with the password 123   abc to log in as a test user  The log on screen is shown in Figure 1 1     Although the screenshot may suggest lots of controls and lots of code to make the security func   tion  in fact with the new security controls in ASP NET 2 0 it   s very easy and not much work at all     If you have not registered  a link will take you to the Sign Up page  depicted in Figure 1 2        Chapter 1       Your Online Diary    User Name       Password       l Remember me next time     Log In    Not registered  Click here to register now   Forgotten your password                 Figure 1 1       Your Online Diary    Sign Up for Your New Account  User Name       Password  Co E    Confirm Password  i   Si  E mail  io   Security Question  EE  Security Answer  ooo o o i    Create User             Figure 1 2  This shows another of the new security controls in ASP NET 2 0  creating a registration process is now  just a matter of adding a control to a form     If you ve forgotten your password  you can click the Forgotten Your Password  link  which directs you  to the Password Reminder wizard pages  see Figure 1 3         Your Online Diary    Forgot Your Password     Enter your User Name to receive your password     User Name       Submit             Figure 1 3    Having logged on  you ar
7.  hold the DiaryEntry object relating to the day being edited   The constructor  shown in the following code  does all the hard work of actually getting the data     Public Sub New ByVal DiaryId As Integer  ByVal EntryDate As Date   mDiaryld   DiaryId  If mDiaryId  gt  0 Then  Try       Dim diaryDBConn As New SqlConnection conString    Dim sqlString As String    GetDiaryEntryByDate    Dim sqlCmd As New SqlCommand sqlString  diaryDBConn   sqlCmd CommandType   CommandType  StoredProcedure    sqlCmd  Parameters  AddWithValue   DiaryId   mDiaryId   sqlCmd Parameters AddWithValue   EntryFromDate   EntryDate   sqlCmd  Parameters  AddWithValue   EntryToDate   EntryDate     diaryDBConn  Open      Dim diaryEntrySQLDR As SqlDataReader    sqlCmd ExecuteReader  CommandBehavior CloseConnection    sqlCmd   Nothing   If diaryEntrySQLDR Read   Then                      mDiaryEntryId   CLng diaryEntrySQLDR  DiaryEntryId      mEntryDate   CDate diaryEntrySQLDR  EntryDate      mEntryTitle   diaryEntrySQLDR  EntryTitle    ToString  mEntryText   diaryEntrySQLDR  EntryText    ToString  Else  mDiaryEntryId    1  mEntryDate   EntryDate  End If  diaryEntrySQLDR Close    diaryEntrySQLDR   Nothing             diaryDBConn Close    diaryDBConn   Nothing       Catch ex As Exception  mDiaryEntryId    1  End Try    End If  End Sub    The GetDiaryEntryByDate stored procedure is called to get the data  If there isn   t an existing entry for    that day  mDiaryEntryTd is set to  1 and all the other properties a
8.  name    LastName String Contact   s surname    Email String Contact   s e mail address    Telephone String Contact   s telephone number   MobilePhone String Contact   s mobile phone number   AddressLinel String Contact   s house name and street address   City String Contact   s city of residence    State String Contact   s state    PostalCode String Contact   s zip or postal code     The ContactCollection Class    The ContactCollection class inherits from the System  Collections  CollectionBase class  The  ContactCollection class   s purpose is simply to store a collection of Contact objects  This class gets    extensive use in the next chapter  when you create a contacts organizer     10    The Online Diary and Organizer  The ContactCollection class has only one property     Property Type Description    Item ByVal Index As Integer  Integer Returns the Contact object stored at the  position in index in the collection     The ContactCollection class   s public methods are as follows     Method Return Type Description    Add  ByVal NewContact As Contact  None Adds a Contact object to  the collection held by the  ContactCollection object     Add ByVal ContactId As Long  None Creates a new Contact object    ContactId is passed to the  Contact object   s constructor  to ensure it   s populated with  the contact   s details from the  database  The new Contact  object is then added to the  collection maintained by the  ContactCollection object     Remove  ByVal Index as Integer  No
9.  sqlCmd  Parameters  AddWithValue   ContactId   ContactId   diaryDBConn  Open     sqlcmd ExecuteNonQuery     diaryDBConn Close    sqlCmd   Nothing  diaryDBConn   Nothing  End Sub    The GridView also includes an Edit link  which when clicked navigates the user to the EditContact aspx  page      lt asp HyperLinkField DataNavigateUrlFields  ContactId   DataNavigateUrlFormatString    SecureDiary EditContact aspx ContactId  0    Text  Edit    gt     The corresponding ContactId is passed in the URL as URL data     Adding a new user involves clicking the Add Contact link on the YourContacts aspx page  This takes  you to a basic form for adding contact information such as name  e mail  phone number  and so on  This  page and the EditContact aspx page are identical in operation except for one important detail  The  EditContact aspx page retrieves the details of the contact to be edited using the Contact class  This hap   pens in the Page_Load event     33    Chapter 1    Protected Sub Page_Load ByVal sender As Object  ByVal e As System EventArgs   Handles Me Load    If IsPostBack Then   Dim currentContact As New   Contact  CLng  Request  QueryString  ContactId      currentContact FirstName   firstNameTextBox Text  currentContact LastName   lastNameTextBox Text  currentContact AddressLinel   addressLinelTextBox  Text  currentContact City   cityTextBox Text  currentContact PostalCode   postalCodeTextBox Text  CurrentContact State   stateTextBox Text  currentContact Telephone   telephon
10.  sqlCmd  Parameters  AddWithValue   DiaryId   mDiaryId    sqlCmd  Parameters  AddWithValue   EntryDate   mEntryDate    sqlcmd  Parameters  AddWithValue   EntryTitle   mEntryTitle    sqlCmd  Parameters  AddWithValue   EntryText   mEntryText    sqlCmd  Parameters  Add   NewDiaryEntryId   SqlDbType BigiInt    sqlCmd  Parameters    NewDiaryEntryId    Direction    ParameterDirection ReturnValue             diaryDBConn  Open     sqlCmd ExecuteNonQuery      mDiaryEntryId   CLng sqlCmd Parameters   NewDiaryEntryId    Value           diaryDBConn Close    sqlcCmd   Nothing    28    The Online Diary and Organizer    diaryDBConn   Nothing  End If  End Sub    The private method UpdateDiaryEntry    updates it     Private Sub UpdateDiaryEntry     If mDiaryEntryld  lt  gt   1 Then  Dim diaryDBConn As New SqlConnection conString   Dim sqlString As String    UpdateDiaryEntry   Dim sqlCmd As New SqlCommand sqlString  diaryDBConn   sqlCmd CommandType   CommandType  StoredProcedure          sqlCmd  Parameters  AddWithValue  sqlCmd  Parameters  AddWithValue  sqlCmd  Parameters  AddWithValue  sqlCmd  Parameters  AddWithValue      DiaryEntryId   mDiaryEntryId     EntryDate   mEntryDate     EntryTitle   mEntryTitle     EntryText   mEntryText                 diaryDBConn  Open     sqlCmd ExecuteNonQuery     diaryDBConn Close    sqlCmd   Nothing  diaryDBConn   Nothing  End If  End Sub       Moving on  the next section discusses aspects of the code dealing with editing  viewing  and deleting  events     
11.  those in the DiaryEvent  object     Shared method that  returns a Boolean array  detailing which days  have events associated  with them  The array  index matches with the  day of the month  1 is the  first of the month  2 the  second  and so on      Shared method   that returns a  SqlDataReader object  populated with rows  from the database detail   ing diary events between  the FromDate and  ToDate arguments     Creates anew Diary  EventCollection  object and populates it  with DiaryEvent objects  whose EntryDate is  between the FromDate  and ToDate arguments        Table continued on following page    15    Chapter 1    Method    DeleteEvent       DeleteEvent  ByVal EventId As Long     Return Type Description    None Deletes from the database the    event with EventId equal to  mEvent Id of the object  The  DiaryEvent object   s values are  re initialized to their defaults     None Shared method that deletes the    event from the database with  an Event Id value equal to the  Event Id argument of the  method     In addition to the constructors and public methods  the DiaryEvent class has these four properties     Property    EventDescription  EventName  EventDate    EventDuration    Description    Description of the event   Short name for the event   Date the event starts     Length of time in minutes that the event lasts     One more class to go  The next section looks at the diary collection handling class        DiaryEventCollection     The DiaryEventCollection Class  
12. 00 SOP REE  Figure 1 7    Clicking that link takes you to the Contact Management page  see Figure 1 8      The Online Diary and Organizer       Your Contacts    Main Page       Last Name First Name Telephone Email Address    Delete Edit Bongela Jon  Delete Edit Twiner Mark    Add New Contact    0023332233 jb another com com  12233444   mtwiner some com          Figure 1 8    Here you see a list of your contacts  which you can edit and delete by clicking the appropriate link in the  Contacts table  You can also add a new contact by clicking the Add New Contact link  which takes you  to the New Contact page  no surprise there    shown in Figure 1 9                 New Contact  First Name Telephone  Last Name Mobile Phone  Address Line1 Email  City  State Save Contact    Cancel  Postal Code  Figure 1 9    Currently the contacts functionality is fairly simple  with such things as linking events and contacts and  automatically e mailing contacts to remind them of an event     Chapter 1    So you   ve seen what the Online Diary does  now you can look at how it does it  The next section  describes the overall design and how the system hangs together  You get a high level tour of the  database setup and each of the classes the system uses     Design of the Online Diary    The diary system is split into the common three layer architecture  All data and direct data modifying  code are in the data layer  a combination of database tables and stored procedures  The data access layer  is e
13. Creating  Editing  and Viewing Diary Events    Events are created by clicking the Add New Event link on the DayView aspx page  This takes you to a  simple form on the AddEvent aspx page  When the Save button is clicked  the button   s click event cre   ates anew DiaryEvent object  populates its properties from the form  and then calls its Save    method   The code flow is much the same as for the DiaryEvent object   s Save    method  Where the functionality  is similar or the same  the names of methods on different objects have been kept the same  It reduces  confusion and makes your life easier     All events relating to a particular day are shown on the DayView aspx page  An ObjectDataSource con   trol on the DayView aspx page draws its data from the DiaryEvent object   s GetDiaryEventsByDate     shared method  The markup for the ObjectDataSource control is shown here      lt asp ObjectDataSource ID  eventsObjectDataSource  runat  server   SelectMethod  GetDiaryEventsByDate   TypeName  DiaryEvent  DeleteMethod  DeleteEvent  gt    lt SelectParameters gt    lt asp SessionParameter DefaultValue   1  Name  DiaryId   SessionField  DiaryId  Type  Int32    gt    lt asp ControlParameter ControlID  dayShownLabel  DefaultValue     Name  FromDate  PropertyName  Text   Type  DateTime    gt    lt asp ControlParameter ControlID  dayShownLabel  DefaultValue     Name  ToDate  PropertyName  Text              29    Chapter 1    Type  DateTime    gt    lt asp Parameter DefaultValue  0  Name  
14. Data Type Allow Nulls      coke ata Type  alow nats    a DisryEntryld bignt r E  Contactid bigi  E   Diaryld rt rc  _  Diaryld int  mi  EntryDate datetime r   FirstName nvarchar S0  F  z EntryTkle nvarchar 50  cr _  LastName nyarchar S0  Vv  E EntryText nvarchar 2000  D LJE Telephone varchar S0  Vv  _  Datechanged datetime r  _  Mobilerhone varchar 50  F  r J Email nyarchar 250  Vv  Ej AddressLine  nvarchar 150  K  Ej cty nvarchar 100  v  _  state nvarchar 100  v  E  PostalCode warchar 50  F  E   m                Figure 1 10    The Online Diary and Organizer    The default database created using the new membership features of ASP NET 2 0 is also used  The  database is a SQL Server Express database and not modified from the one created by Visual Studio  Express  However  to link the log on and the diary details  the UserName field in the DiaryDB database  takes its value originally from the membership database  You go through this in more detail shortly   Membership details are contained in the ASPNETDB database that Visual Web Developer Express cre   ates for you  Although it contains quite a few tables  you never access them via the code in this project   It   s accessed exclusively by the new Login controls     it does all the hard work behind the scenes     This project only makes use of the aspnet_Users table  shown in Figure 1 11  to provide log on security  checking and provide a username for the main DiaryDB  You may well want to extend the membership  database to incl
15. MaxRows  Type  Int32    gt    lt  SelectParameters gt    lt DeleteParameters gt    lt asp Parameter Name  EventId  Type  Int64    gt    lt  DeleteParameters gt    lt  asp ObjectDataSource gt     Notice that the Select Parameters and the DeleteParameters are set to specify the data passed to the  GetDiaryEventsByDate   method used to pull back the data  and the DeleteEvent    method is used  to delete diary events     A GridView control is hooked to the ObjectDataSource in the code above      lt asp GridView ID  eventsGridView  runat  server   AutoGenerateColumns  False  CellPadding  4   DataSourceID  eventsObjectDataSource  ForeColor   333333   GridLines  None  Height  1px   PageSize  5  Style  z index  108  left  78px  position  absolute  top   357px        Width  542px  DataKeyNames  EventId  gt    lt FooterStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White    gt    lt RowStyle BackColor   F7F6F3  ForeColor   333333    gt    lt Columns gt    lt asp HyperLinkField DataNavigateUrlFields  EventId  Text  Edit   DataNavigateUrlFormatString    SecureDiary EditEvent aspx EventId  0     gt    lt asp CommandField ShowDeleteButton  True    gt    lt asp BoundField DataField  EventName  HeaderText  Event    gt    lt asp BoundField DataField  EventDescription   HeaderText  Description    gt    lt  Columns gt    lt PagerStyle BackColor   284775  ForeColor  White   HorizontalAlign  Center    gt    lt SelectedRowStyle BackColor   E2DED6  Font Bold  True   ForeColor   333333    gt 
16. The DiaryEvent class also has two Delete   methods  One is a shared method and therefore  doesn   t require a DiaryEvent to be created  and requires an Event Id parameter  It   s used by some of  the built in data access components provided with ASP NET 2 0  The second is an object method that  deletes the event referenced by the current DiaryEvent object  As well as enabling the details of one  diary entry to be retrieved  the DiaryEvent class provides additional methods for getting details of a  number of diary events as either a collection or as a DataSet by returning a Sq1DataReader object                 The Online Diary and Organizer    The following table explains these methods in detail     Method Return Type  Save    None  GetDaysInMonthWithEvents  ByVal Boolean Array  DiarylId As Integer  ByVal   Month As Integer  ByVal Year   As Integer    GetDiaryEventsByDate  ByVal DiaryId SqlDataReader    As Integer  ByVal FromDate As Date     ByVal To       Date As Date        GetDiaryEventsByDateAsCollection ByVal DiaryEventCollection  DiaryId As Integer  ByVal FromDate As  Date  ByVal ToDate As Date     Description    Saves a fully populated  DiaryEvent object  If it   s  a new entry  Save    calls  InsertNew DiaryEvent  sub and the details are  inserted into the data   base  The new Event Id  is returned from the  database and entered   in to mEventId    If the entry already   exists in the database   Save    calls  UpdateDiaryEvent   which updates the  database values with 
17. and a GridView control  the principles being  identical to the displaying  deleting  and editing of the diary events  This time the Contact class is used  for editing and display contact details  but otherwise the code is very similar to the events code     The main page for displaying contacts is YourContacts aspx  which contains a GridView control in  which all current contacts are listed      lt asp GridView ID  GridViewl  runat  server  AutoGenerateColumns  False   CellPadding  4   DataSourceID  ObjectDataSourcel  ForeColor   333333  GridLines  None   Style  z index  101   left  36px  position  absolute  top  137px  DataKeyNames  ContactId  gt    lt FooterStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White    gt    lt Columns gt    lt asp CommandField ShowDeleteButton  True    gt    lt asp HyperLinkField DataNavigateUrlFields  ContactId   DataNavigateUrlFormatString    SecureDiary EditContact aspx ContactId  0    Text  Edit    gt    lt asp BoundField DataField  LastName  HeaderText  Last Name    gt    lt asp BoundField DataField  FirstName  HeaderText  First Name    gt    lt asp BoundField DataField  Telephone  HeaderText  Telephone    gt    lt asp BoundField DataField  Email  HeaderText  Email Address    gt    lt  Columns gt    lt RowStyle BackColor   F7F6F3  ForeColor   333333    gt    lt EditRowStyle BackColor   999999    gt    lt SelectedRowStyle BackColor   E2DED6  Font Bold  True   ForeColor   333333    gt    lt PagerStyle BackColor   284775  ForeColor  Whit
18. awing Color Red  End If    End If  End Sub    The first 1   block in the preceding event code deals with ensuring entryArrayOfDays and  eventArrayOfDays are populated with details of which days have an associated event or diary entry   They are both Boolean arrays  if a day has an event or entry  the array element for that day contains  True  Arrays are populated by the DiaryEnty and DiaryEvent classes    shared functions  GetDaysInMonthWithEntries   and GetDaysInMonthWithEvents             In the second If block of the event the code checks to see whether the day of the month being rendered  has a diary event or diary entry  If there   s an event  the day   s text is set to red  If there   s a diary entry the  day   s background is rendered in blue     As well as a Calendar control  the main page also has two GridView controls  discussed a bit later    The upper one displays upcoming events  the lower one displays recent diary entries  Both GridView  controls get their data from an ObjectDataSource control  new to ASP NET 2 0  In the past  data source  controls have interacted directly with the database  They are nice and easy to use     put on one a page   set a few properties  drop in a few data aware controls  and away you go  However  that   s not actually  good coding practice  Splitting up the data access  business  and presentation layers is generally consid   ered good practice  but means leaving behind nice and easy to use data source controls     The Online Diary a
19. bjectDataSource control  In addition  the markup specifies which columns to display by setting  AutoGenerateColumns to False  A final step is to create a list of columns      lt Columns gt    lt asp BoundField DataField  EntryDate    gt    lt asp BoundField DataField  EntryTitle    gt    lt asp BoundField DataField  EntryText    gt    lt  Columns gt        As well as enabling the display of data  the ObjectDataSource control can also update  insert  and  delete records from a database  as demonstrated shortly     Creating  Editing  and Viewing a Diary Entry    The DayView aspx page allows for diary editing  This page contains a simple form allowing you to enter  title and diary entry details  It also displays any existing diary entry     All of the hard work is done by use of the DiaryEntry class  Its Page_Load event creates a new  DiaryEntry class  passing its constructor the current user   s DiaryId and also the date the page  refers to        Protected Sub Page_Load ByVal sender As Object  ByVal e As System EventArgs    Handles Me Load  mDiaryEntry   New DiaryEntry CInt  Session  DiaryId       CDate  dayShownLabel   Text     changeDayCalendar SelectedDate   CDate dayShownLabel  Text   changeDayCalendar VisibleDate   changeDayCalendar SelectedDate  If Not IsPostBack Then   entryTextTextBox Text   mDiaryEntry EntryText  entryTitleTextBox Text   mDiaryEntry EntryTitle  End If  End Sub             26    The Online Diary and Organizer    mDiaryEntry is a global variable used to
20. convention    means the purpose of each stored procedure doesn   t need a lot of explanation  As the code is discussed   you look at the stored procedures in more detail where necessary     The Business Layer    The business layer is organized into seven classes  The four main classes are as follows     OnlineDiary    DiaryEntry       DiaryEvent       Oooo    Contact    These classes do most of the work of temporarily holding diary related data and retrieving and storing  it in the database  There are also three collection classes  The first order of business is the OnlineDiary  class     The OnlineDiary Class    This class contains only two shared public methods  detailed in the following table     Method Return Type Description   InsertDiary  ByVal UserName As String  None Inserts a new diary user   ByVal FirstName As String  ByVal into the OnlineDiary   LastName As String  database    GetDiaryIdFromUserName  ByVal Integer Looks up the UserName   UserName As String  in the database and returns  the associated Diaryld     The purpose of the OnlineDiary class is simply to provide a couple of handy shared methods relating  to an online diary as a whole  It could also be used to expand the diary system and add new functional   ity that relates to the overall diary system  rather than a specific part such as contacts     The Contact Class    The Contact class objectifies a single contact    a person or thing for which you want to store contact  information  It encapsulates every
21. d in this chapter is the ObjectDataSource control  In the past data  source controls have made life nice and easy  But they were quick and dirty  which meant poor code  design  and you had to wave goodbye to a three tier architecture  Now with the ObjectDataSource  control you can have quick and dirty and three tier architecture     great news for creating easily main   tainable  well designed projects     In the next chapter you will be creating a file sharing project and learning some more about ASP NET 2 0   s  great new features     35    
22. e   HorizontalAlign  Center    gt    lt HeaderStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White    gt    lt AlternatingRowStyle BackColor  White  ForeColor   284775    gt    lt  asp GridView gt           32    The Online Diary and Organizer    It gets its data from the ObjectDataSource control Obj ectDataSourcel1  which in turn connects to the  Contact class   s GetContactByFirstLetter   shared method      lt asp ObjectDataSource ID  ObjectDataSourcel  runat  server   SelectMethod  GetContactsByFirstLetter   TypeName  Contact  DeleteMethod  DeleteContact  gt    lt SelectParameters gt    lt asp SessionParameter DefaultValue  6  Name  DiaryId   SessionField  DiaryId  Type  Int32    gt    lt asp Parameter Name  FirstLetterOfSurname  Type  Char    gt    lt  SelectParameters gt    lt DeleteParameters gt    lt asp ControlParameter ControlID  GridViewl  Name  ContactId   PropertyName  SelectedValue   Type  Int64    gt    lt  DeleteParameters gt    lt  asp ObjectDataSource gt        The ObjectDataSource control   s DeleteMethod parameter is also hooked to the Contact class   s  DeleteContact  The GridView control has been set to show a link to delete each contact  and it   s this  method that does the actual deleting     Public Shared Sub DeleteContact  ByVal ContactId As Long   Dim diaryDBConn As New SqlConnection conString   Dim sqlString As String    DeleteContact   Dim sqlCmd As New SqlCommand sqlString  diaryDBConn   sqlCmd CommandType   CommandType  StoredProcedure   
23. eTextBox Text  currentContact MobilePhone   mobilePhoneTextBox  Text  currentContact Email   emailTextBox Text  currentContact SaveContact     currentContact   Nothing  Response Redirect   YourContacts aspx                           fh Gh   GtheGh eGhanGhieGtanGth Gh R E    Else       Dim currentContact As New  Contact  CLng  Request  QueryString  ContactId        firstNameTextBox Text   currentContact FirstName  lastNameTextBox Text   currentContact LastName  addressLinelTextBox Text   currentContact AddressLinel  cityTextBox Text   currentContact City  postalCodeTextBox Text   currentContact PostalCode  stateTextBox  Text   currentContact State  telephoneTextBox Text   currentContact Telephone  mobilePhoneTextBox Text   currentContact MobilePhone  emailTextBox Text   currentContact Email  currentContact   Nothing  End If  End Sub          The If statement determines whether this is a postback  the form has been submitted to itself  or  whether the page has just been loaded  If it   s a postback  you need to save the data and then move back  to the main contacts section  If it   s a new page load  it   s necessary to create a new Contact object  and  use the data from that to populate the form fields with the contact information     The AddContact aspx page is identical except there   s no need to populate with existing contact data   because a new contact has no prior data     Setting up the Online Diary    One of the great things about ASP NET 2 0 is how easy it is to set 
24. ecified in CreateUserUrl     Finally  just in case the user has already registered but forgotten his or her password  the  PasswordRecoveryText attribute displays a    Forgotten your password     message and  PasswordRecoveryUr1 sets the URL the users are navigated to if they need to find out their password     The only code you need to write is in the Login control   s LoggedIn event  which fires if the user suc   cessfully enters a username and password     Protected Sub Loginl_LoggedIn ByVal sender As Object  ByVal e As  System EventArgs  Handles Loginl LoggediIn  Dim DiaryId As Integer   GetDiaryIdFromUserName  Login1  UserName   Session  DiaryId     DiaryId  End Sub    This uses the supplied username to look up the user   s DiaryId in the Online Diary database  This is  then stored in the session variable     The SignOn aspx page also allows new users to register     New User Registration    The RegisterStart aspx  page deals with the registration of a new user  As with SignOn aspx  this page  also uses one of the new Login controls  this time the CreateUserWizard control  The markup for the  CreateUserWizard control is shown in the following code      lt asp CreateUserWizard ID  CreateUserWizardi  runat  server    BackColor   F7F6F3  BorderColor   E6E2D8    BorderStyle  Solid  BorderWidth  1px  Font Names  Verdana  Font   Size  0 8em    Style  z index  100  left  66px  position  absolute  top  43px   Height  164px  Width  300px  FinishDestinationPageUrl    SignOn aspx  
25. es  and password  reminder pages  basically anything that requires you to be logged in to view     Registration  Logging On  and Security    The Online Diary application uses the new Login controls to provide the diary   s user handing features   including new user registration  log in  and password reminder     The Login controls are a real time saver  allowing a lot of sophisticated functionality to be added with  just a little work and hardly any code  ASP NET 2 0 has seven new security or login controls     Q Login  Enables users to log in and verifies username and password        Q Loginview  Enables the display of different templates depending on whether a user is logged in  and also his or her role membership      m     PasswordRecovery  Provides password reminder functionality for users who forget their  password     LoginStatus  Displays whether a user is logged in or out        LoginName  Displays currently logged in username     CreateUserWizard  Creates a new user wizard     registration of a new user in simple steps        Ooo o    ChangePassword  Enables users to change their password     The Online Diary project  however  use only the Login  LoginName  CreateUserWizard  and  ChangePassword controls     Logging On  The SignOn aspx page contains a Login control  The user database is created using the web site admin   istration tools  This goes through the steps needed one by one  and once it   s finished a new database  called ASPNETDB MDF appears in the App_Data
26. gt     lt SideBarStyle BackColor   5D7B9D  BorderWidth  0px  Font Size  0 9em   VerticalAlign  Top    gt     lt SideBarButtonStyle BorderWidth  0px  Font Names  Verdana   ForeColor  White    gt     19    Chapter 1    20     lt NavigationButtonStyle BackColor   FFFBFF  BorderColor   CCCCCC   BorderStyle  Solid   BorderWidth  1px  Font Names  Verdana  ForeColor   284775    gt    lt HeaderStyle BackColor   5D7B9D  BorderStyle  Solid  Font Bold  True   Font Size  0 9em   ForeColor  White  HorizontalAlign  Left    gt    lt CreateUserButtonStyle BackColor   FFFBFF  BorderColor   CCCCCC   BorderStyle  Solid   BorderWidth  1px  Font Names  Verdana  ForeColor   284775    gt    lt ContinueButtonStyle BackColor   FFFBFF  BorderColor   CCCCCC   BorderStyle  Solid   BorderWidth  1px  Font Names  Verdana  ForeColor   284775    gt    lt StepStyle BorderWidth  0px    gt    lt TitleTextStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White       gt    lt WizardSteps gt    lt asp CreateUserWizardStep runat  server  gt    lt  asp CreateUserWizardStep gt    lt asp WizardStep ID  personalDetailsStep  runat  server  Title  User  Details  gt      lt table border  0  style  font size  100   font family   Verdana  z index  100  left  Opx  position  absolute  top  Opx   gt   KEYES   lt td align  center  colspan  2  style  font weight   bold  color  white  background color   5d7b9d  gt   Your Personal Details lt  td gt    lt  tr gt   KETS   lt td align  right  style  height  26px  gt    lt label f
27. here  and you do that in the CreateUserWizard control   s FinishButtonClick event     Protected Sub CreateUserWizardl_FinishButtonClick ByVal sender As Object  ByVal  e As System Web UI WebControls WizardNavigationEventArgs  Handles  CreateUserWizard1 FinishBu    Dim myTextBox As T  Dim UserName  Firs  myTextBox   Create  FirstName   myText  myTextBox   Create  LastName   myTextB    ttonClick   extBox   tName  LastName   UserWizard1 FindControl   firstNameTextBox     Box  Text   UserWizard1 FindControl   lastNameTextBox     ox  Text    UserName   CreateUserWizard1 UserName    OnlineDiary Insert    End Sub    22       Diary UserName  FirstName  LastName     The Online Diary and Organizer    This step creates a new diary for users and stores their first and last names  The UserName comes from  the CreateUserWizard control   s UserName property  and then uses the shared method InsertDiary     to insert the new user in the Online Diary   s database     Being human  sometimes people forget their passwords  Fortunately  ASP NET 2 0 comes with the capa   bility to refresh overloaded memories     Password Reminder    Again with virtually no code  you can create a fully functional password reminder feature for the Online  Diary  this time courtesy of the PasswordRecovery control  Virtually all of its settings are at the default   values or simply related to style  Even better  there   s just one line of code and that   s in the SendingMail  event     Protected Sub PasswordRecoveryl_Se
28. ifies the primary  key field that needs to be used to delete the row     Returning to editing the event  When you click the Edit link you are taken to the EditEvent aspx page   The clicked Edit link   s Event Id is passed as a URL parameter  The EditEvent aspx page is virtually iden   tical to the AddEvent aspx page discussed previously  The main difference is when the page initializes   The Page_Init event handler is shown in the following code  and it   s here that the event details are  entered into the form     Protected Sub Page_Init  ByVal sender As Object  ByVal e As System EventArgs   Handles Me Init       Dim EventBeingEdited As New   DiaryEvent  CLng  Request  QueryString  EventId        eventNameTextBox Text   EventBeingEdited EventName  eventDescriptionTextBox Text   EventBeingEdited EventDescription  dayShownLabel Text   EventBeingEdited EventDate Day  amp       amp    MonthName  EventBeingEdited EventDate Month   amp       amp  EventBeingEdited EventDate  Year                         Dim NewListItem As ListItem  HourCount  MinuteCount    For HourCount   0 To 23  If HourCount  lt  10 Then  NewListItem   New ListItem  0   amp  HourCount  HourCount ToString   Else  NewListItem   New ListItem HourCount ToString  HourCount ToString   End If  If EventBeingEdited EventDate Hour   HourCount Then  NewListItem Selected   True  Bnd  StartHourDropDownList Items  Add NewListItem   Next          For MinuteCount   0 To 59  If MinuteCount  lt  10 Then  NewListItem   New ListIte
29. ior CloseConnection     sqlCmd   Nothing  Return entrySQLDR    End Function    The method returns a Sq1DataReader object populated with the data the ObjectDataSource control    will use     Actually displaying the data is then just a matter of pointing a data aware control at the  ObjectDataSource      lt asp GridView ID  recentEntriesGridView  runat  server     AutoGenerateColumns  False     Caption  Recent Entries  CaptionAlign  Left  CellPadding  4     DataSourcelID  diaryEntriesObjectDataSource     ForeColor   333333  GridLines  None  Style  z index  105  left     535px  position  absolute     Ws    top  321px  Width  476px  Height  208px  gt    lt FooterStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White      lt RowStyle BackColor   F7F6F3  ForeColor   333333    gt    lt Columns gt    lt asp BoundField DataField  EntryDate    gt    lt asp BoundField DataField  EntryTitle    gt     25    Chapter 1     lt asp BoundField DataField  EntryText    gt     lt  Columns gt     lt PagerStyle BackColor   284775  ForeColor  White   HorizontalAlign  Center    gt     lt SelectedRowStyle BackColor   E2DED6  Font Bold  True   ForeColor   333333    gt     lt HeaderStyle BackColor   5D7B9D  Font Bold  True  ForeColor  White     gt     lt EditRowStyle BackColor   999999    gt     lt AlternatingRowStyle BackColor  White  ForeColor   284775    gt     lt  asp GridView gt     In the GridView control   s markup  the DataSourceID attribute specifies the source of the data  which is  the O
30. le line of  every page  but rather it takes a general overview of how the application works and dives a bit deeper  where necessary     Discussion of the project is approached in a functionality based way  Instead of discussing a specific  page and what it does  the following sections discuss a process     such as registration     and how it   s    achieved     It begins with an overview of the files and file structure     File Structure    An overview of the file structure is shown in Figure 1 12        E   Sa EAA TITS TETE AN  E  L App_Code   Y2  Contact  vb   Y2  ContactCollection vb  Ye  DiaryEntry vb   Y2  DiaryEntryCollection vb  Y2  DiaryEvent vb   Y2  DiaryEventCollection vb  Y2  OnlineDiary vb  App_Data   T  ASPNETDB MDF    J  DiaryDB mdf   y SecureDiary   ES  AddContact aspx    i  AddEvent aspx    i  DayYiew  aspx  DiaryMain  aspx  EditContact aspx  EditEvent aspx  web config  YourContacts  aspx    T  A A A   E E    Pi    EEF EA                         H  E  PasswordReminder aspx  H   3  RegisterStart aspx  WH  E  SignOn  aspx    7  5 web config  Figure 1 12    17    Chapter 1    Each of the seven class files is stored in the App_Code directory  at the top of the figure   The App_Data  directory contains the two databases  the login database  ASPNETDB MDF  and the Online Diary  database  DiaryDB mdf   Pages that require you to log in before viewing are stored separately in the  SecureDiary directory  Finally  the root directory contains login pages  registration pag
31. m  0   amp  MinuteCount ToString   MinuteCount ToString   Else  NewListItem   New ListItem MinuteCount ToString   MinuteCount ToString   End If  If EventBeingEdited EventDate Minute   MinuteCount Then  NewListItem Selected   True  End If  StartMinuteDropDownList Items Add NewListItem   Next  Dim itemToSelect As ListItem                   31    Chapter 1    itemToSelect    eventDurationDropDownList Items FindByValue  EventBeingEdited EventDuration ToString               itemToSelect Selected   True    EventBeingEdited   Nothing  End Sub    The Event Id is extracted from the URL parameters and used to create a new DiaryEvent object   Populating the event text boxes is easy enough  but the details of time and duration of the event involve  populating the Hour and Minute drop down boxes and ensuring the correct value is selected  This is  achieved by looping through hours from 0 to 23 and then minutes from 0 to 59  If the hour to be added  to the list is the same as the hour about to be added to the list box  make sure it   s the default selected  one  The same goes for the minute list box population        Managing Contacts    Managing contacts is the last aspect of the Online Diary you ll examine  and uses many of the same  principles as the other sections  YourContacts aspx is the central contact management page  Here a list of  current contacts is displayed  and the option to add  edit  and delete contacts is possible     All contacts are displayed using a DataObj ect Source 
32. nd Organizer    However  the new ObjectDataSource lets you have the best of both  easy to use data controls and use of  classes to separate business  data  and presentation layers  Instead of connecting directly to a database   the ObjectDataSource takes its data from one of the classes  diaryEntriesObjectDataSource on  DiaryMain aspx  for example  takes its data from the GetDiaryEntriesRecentlyChanged   method  of the DiaryEntry class  whose markup is shown here         lt asp ObjectDataSource ID  diaryEntriesObjectDataSource  runat  server     SelectMethod  GetDiaryEntriesRecentlyChanged     TypeName  DiaryEntry  gt    lt SelectParameters gt    lt asp SessionParameter DefaultValue   1  Name  DiaryId     SessionField  DiaryId  Type  Int32    gt      lt  SelectParameters gt    lt  asp ObjectDataSource gt     The TypeName attribute specifies the class name to use  and the SelectMethod attribute specifies which  method of that class will provide the data  GetDiaryEntriesRecentlyChanged   is a shared method     shown here     Public Shared Function GetDiaryEntriesRecentlyChanged ByVal DiaryId As Integer     As SqlDataReader    Dim diaryDBConn As New SqlConnection conString    Dim sqlString As String    GetRecentDiaryEntries   Dim sqlCmd As New SqlCommand sqlString  diaryDBConn   sqlCmd CommandType   CommandType  StoredProcedure    sqlCmd  Parameters  AddWithValue   DiaryId   DiaryId     diaryDBConn  Open     Dim entrySQLDR As SqlDataReader      sqlCmd ExecuteReader  CommandBehav
33. ndingMail  ByVal sender As Object  ByVal e As  System Web UI WebControls MailMessageEventArgs  Handles  PasswordRecoveryl  SendingMail  returnToLogOnHyperLink Visible   True  End Sub       The SendingMail event fires when the user presses the Send Email button and simply displays the  Return to Main Page link  rather than leaving the user guessing as to where to go next     The main work involved is configuring the SMTP server settings that ll be used to actually send the  password reminder e mail  Visual Web Developer doesn   t come with an SMTP server  However  if you  are using Windows XP or 2000  all you need to do to install one is go to the Start   Settings gt Control  Panel   Add or Remove Programs  From there  select Add Remove Windows Components  Select the  Internet Information Server  IIS  option and click Details at the bottom right of the dialog  In the result   ing dialog box  you ll see a list  Check the box next to SMTP Service and click OK  Then click Next to  install an SMTP service     Once the SMTP service is installed  add the following shaded code between the  lt configuration gt  tags  in the Web config file      lt configuration xmlns  http   schemas microsoft com  NetConfiguration v2 0  gt    lt connectionStrings gt     lt add name  DiaryDBConnectionString  connectionString  Data  Source   SQLEXPRESS   AttachDbFilename  DataDirectory  DiaryDB mdf  Integrated  Security True User Instance True    providerName  System Data SqlClient    gt    lt  connecti
34. ne Removes the Contact object  from the collection at the speci   fied index     That deals with the Contact classes  now take a look at the two classes dealing with diary entries     The DiaryEntry Class    The DiaryEntry class objectifies a single entry in a diary  It encapsulates everything to do with diary  entries  including creating  updating  and retrieving diary entry data  It handles all the database access  for diary entries     It has three constructors  outlined in the following table        Constructor Description   New ByVal DiaryId as Integer  Creates a new DiaryEntry object with all properties  set to their default values    New  ByVal DiaryEntryId As Long  Creates a new DiaryEntry object with its properties  retrieved from the database using the argument  DiaryEntrylId    New  ByVal DiaryId AS Integer  Creates a new DiaryEntry object with its properties   ByVal EntryDate As Date  retrieved from the database using the arguments       DiarylId and EntryDate     11    Chapter 1    Having created a DiaryEntry object  saving it involves simply calling the Save    method  As with the  Save   method of the Contacts class  the DiaryEntry class will work out whether it   s a new diary  entry that needs to be inserted into the database  or an existing entry that needs to be updated  As well  as enabling retrieval of one diary entry   s details  the DiaryEntry class provides additional methods for  getting details of a number of diary entries as either a collection or a
35. new  DiaryEntry  Collection   object and populates  it with DiaryEntry  objects whose  EntryDate is  between the  FromDate and  ToDate arguments     Returns a  SqlDataReader  containing a DataSet  of diary entries  recently created     In addition to the constructors and methods  the DiaryEntry class contains the following properties        Property Type  EntryTitle String  EntryText Siren Gj  EntryDate Date    Description    Title for the day   s diary entry     Text of the day   s diary entry     Date the entry was posted     The other class dealing with diary entries is the DiaryEntryCollection class  which is explained next     The DiaryEntryCollection Class    The DiaryEntryCollection class inherits from the System  Collections CollectionBase class  Its  purpose is simply to store a collection of DiaryEntry objects        This class contains only one property  described in the following table     Property Type  Item ByVal Index As integer  Integer     Description    Returns the DiaryEntry object stored  at the specified position in index in the    collection     13    Chapter 1    Th    14    Along with the Item   property  the DiaryEntryCollection class has three public methods     Method Return Type    Add  ByVal New DiaryEntry None  As DiaryEntry        Add  ByVal DiaryEntryId None  As Long   Remove  ByVal Index as None  Integer     Description    Adds a DiaryEntry object to the collection  held by the DiaryEntryCollection  object        Creates a new DiaryEntry objec
36. onStrings gt     lt system web gt     lt roleManager enabled  true    gt    lt authentication mode  Forms   gt    lt compilation debug  true   gt  lt  system web gt     lt system net gt      lt mailSettings gt    lt smtp from  system diary system com  gt    lt network host  localhost  password   userName      gt    lt  smtp gt      lt  mailSettings gt    lt  system net gt    lt  configuration gt        23    Chapter 1    Viewing the Online Calendar    24    The DiaryMain aspx page is the central hub of the application  It displays a calendar of the current  month  showing which days have events or diary entries associated with them  It also displays a list of  upcoming events and diary entries for the current month     To display when a day has events or a diary entry  the OnDayRender event of the Calendar control  is used     Protected Sub Calendarl_OnDayRender  ByVal sender As Object  ByVal e As  System Web UI  WebControls DayRenderEventArgs  Handles Calendar1 DayRender  If Not e Day IsOtherMonth Then  If entryArrayOfDays Is Nothing Then  entryArrayOfDays   GetDaysInMonthWithEntries Session  DiaryId     e Day Date Month  e Day Date Year   End If       If eventArrayOfDays Is Nothing Then  eventArrayOfDays   GetDaysInMonthWithEvents Session  DiaryId     e Day Date Month  e Day Date  Year   End If    If entryArrayOfDays  CInt e Day DayNumberText   Then  e Cell BackColor   Drawing Color Blue  End If    If eventArrayOfDays  CInt  e Day DayNumberText   Then  e Cell ForeColor   Dr
37. or  UserName  gt   Your First Name  lt  label gt  lt  td gt    lt td style  width  179px  height  26px  gt    lt asp TextBox ID  firstNameTextBox   runat  server  CausesValidation  True  gt  lt  asp TextBox gt  amp nbsp    lt  td gt   Ae  SELA   lt td align  right  gt    lt label for  Password  gt   Your Last Name  lt  label gt  lt  td gt    lt td style  width  179px  gt    lt asp TextBox ID  lastNameTextBox   runat  server  CausesValidation  True  gt  lt  asp TextBox gt  amp nbsp      lt  td gt   aeS  RELA    lt td align  center  colspan  2  style  height    18px  gt    amp nbsp   lt  td gt    af eS  LETS     lt td align  center  colspan  2  style  color  red  gt    amp nbsp   lt  td gt   Aice    The Online Diary and Organizer     lt  table gt    lt  asp WizardStep gt    lt asp CompleteWizardStep runat  server  gt    lt ContentTemplate gt    lt table border  0  style  font size  100   width  383px   font family  Verdana  height  164px  gt   SiEigs    lt td align  center  colspan  2  style  font weight   bold  color  white  background color   5d7b9d  gt     Complete lt  td gt   sheis   lt tr gt     lt td gt   Your account has been successfully  created  lt  td gt    s e   lt tr gt      lt td align  right  colspan  2  gt    lt asp Button ID  ContinueButton  runat  server   BackColor   FFFBFF  BorderColor   CCCCCC   BorderStyle  Solid  BorderWidth  1px   CausesValidation  False  CommandName  Continue   Font Names  Verdana  ForeColor   284775   Text  Continue  ValidationGroup  C
38. re left at their default values   Otherwise they are populated with the data from the database     27    Chapter 1    When the diary title or entry boxes are changed  mDiaryEntry is updated     Protected Sub entryTitleTextBox_TextChanged  ByVal sender As Object  ByVal e As  System EventArgs  Handles entryTitleTextBox TextChanged  mDiaryEntry EntryTitle   entryTitleTextBox Text  End Sub          Protected Sub entryTextTextBox_TextChanged  ByVal sender As Object  ByVal e As  System EventArgs  Handles entryTextTextBox TextChanged  mDiaryEntry EntryText   entryTextTextBox Text  End Sub          Saving changes occurs when you click the Save button        Protected Sub saveDiaryEntryButton_Click ByVal sender As Object  ByVal e As  System EventArgs  Handles saveDiaryEntryButton Click  mDiaryEntry Save    End Sub             All that   s involved is calling the Save    method of the DiaryEntry object     Public Sub Save    If mDiaryEntryId    1 Then  InsertNewDiaryEntry     Else  UpdateDiaryEntry     End If  End Sub       Based on whether or not mDiaryEntryTd is  1  the method either inserts a new entry into the database  or updates an existing one  The private method InsertNewDiaryEntry   inserts a new diary entry        Private Sub InsertNewDiaryEntry     If mDiaryId  lt  gt   1 Then  Dim diaryDBConn As New SqlConnection conString   Dim sqlString As String    InsertDiaryEntry   Dim sqlCmd As New SqlCommand sqlString  diaryDBConn   sqlCmd CommandType   CommandType StoredProcedure   
39. reateUserWizard1    gt    lt  td gt   ie   lt  table gt    lt  ContentTemplate gt    lt  asp CompleteWizardStep gt    lt  WizardSteps gt    lt  asp CreateUserWizard gt     Most of the markup and attributes relate to style settings  However  one essential attribute is the  FinishDestinationPageUrl  This is where the user is taken once the registration process is com   pleted  in the Online Diary it   s the SignOn aspx page    You   ve probably noticed a number of WizardStep tags in the markup  such as this one      lt asp WizardStep ID  personalDetailsStep  runat  server  Title  User Details  gt     The CreateUserWizard works on a step by step basis  There must be least one step that allows the user  to choose a username and password and various security questions  see Figure 1 13      This step and its style can be modified  but Figure 1 13 shows its default value  The control takes care of  inserting the new user data into the user database     A second step  shown in Figure 1 14  is displayed after the user is created     21    Chapter 1       c       Sign Up for Your New Account    Security Question     Your Online Diary    User Name     E   Password   onfirm Password     E mail       Security Answer     Create User          Figure 1 13       oy       Your Personal Details  Your First Name     Your Online Diary    our Last Name     Finish          Figure 1 14    This screen asks users for their first name and last name  This time it   s up to you to store the data some   w
40. rive at the main diary page  as displayed in Figure 1 4     The Online Diary and Organizer             Address    http   localhost  1060 OnlineDiary SecureDiary  DiaryMain aspx X Go Links 7              Your Online Diary You Are Logged On As  juserS    Manage Your Contacts       Upcoming Events  Event Date Event Description  25 12 2005 09 00 00 Chnstmas Day Unwrap presents  Mon Tue Wed Thu Fri Sat Sun  2   1 2 3 4  z     6 a 8 g 10 il    Recent Entries  13 14 15 16 17 18    31 October 2005 Brakes on car work intermittently While I love th                 192 20 21 22 23 24 25  29 October 2005 Steering wheel came off Steering wheel  26 27 28 29 30 31 1 5 October 2005    test entry your text here     24 October 2005 Bought new car Bought a new ca  z 7   2 J    21 October 2005 First logged on to my diary Today I logged     Figure 1 4    On this page you see a monthly calendar  Days with diary entries are marked with a blue background   Days with events are marked in red text  Notice also on the right that upcoming events are highlighted   as are recent diary entries     Clicking on a day moves you through to the area where you can enter your diary entry for that day  and  add  edit  and delete events  see Figure 1 5                              29 October 2005 s Tai  r r Main Diary Page  Entry Title  Steering wheel came off Sep October 2005 Nov                 _  Mon Tue Wed Thu Fri Sat Sun  Diary Text  Steering wheel came off the car which made driving quite a    26 27 28 29 30
41. s a DataSet by returning a  sqlDataReader object  The methods of this class are explained in the following table        Method Return Type Description    Save    None Saves a fully populated  DiaryEntry object  If it   s a  new entry  Save    calls  InsertNewDiaryEntry  sub and the details are  inserted in to the database   The new DiaryEntrylId is  returned from the database  and entered in to mDi   aryEntrylId    If the entry already exists  in the database  Save     calls UpdateContact   which updates the  database values with those  in the DiaryEntry object     GetDaysInMonthWithEntries  ByVal Boolean Array Shared method that  DiaryId As Integer  ByVal Month returns a Boolean   As Integer  ByVal Year As   Integer  array detailing which days    have a diary entry associ   ated with them  The array  index matches with the  day of the month  1 is the  first of the month  2 the  second  and so on      GetDiaryEntriesByDate  ByVal SqlDataReader Shared method   DiaryId As Integer  ByVal that returns a   FromDate As Date  ByVal ToDate SQLDataReader object   As Date  populated with rows from  the database detailing    diary entries between the  FromDate and ToDate  arguments     12    Method    GetDiaryEntriesByDateAsCollection  ByVal  DiaryId As Integer  ByVal FromDate  As Date  ByVal ToDate As Date     GetDiaryEntriesRecentlyChanged  ByVal  DiarylId As Integer     The Online Diary and Organizer    Return Type    DiaryEntryCollection    SqlDataReader    Description    Creates a 
42. t   DiaryEntryId is passed to the  DiaryEntry object   s constructor to ensure  it   s populated with the diary entry   s details  from the database  The new DiaryEntry  object is then added to the collection main   tained by the DiaryEntryCollection  object     Removes the DiaryEntry object from the  collection at the specified index     So far the classes dealing with contacts and diary entries have been discussed  The next section discusses    the diary events     e DiaryEvent Class    The DiaryEvent class objectifies a single entry in a diary  It encapsulates everything to do with diary  entries  including creating  updating  and retrieving diary events data  It handles all the database access    for diary events     The DiaryEvent class has three constructors  outlined as follows     Constructor Description    New  ByVal Diaryid as Integer  Creates anew DiaryEvent object with all properties set to  their default values     New ByVal EntryId As Long  Creates a new DiaryEvent object with its properties  retrieved from the database using the argument Event Id     New  ByVal DiaryId AS Integer  Creates anew DiaryEvent object with its properties  ByVal EventDate As Date  retrieved from the database using a combination of the  arguments DiaryId and EventDate     Having created a DiaryEvent object  saving it involves simply calling the Save    method  The class  will work out whether it   s a new diary event to insert into the database  or an existing one in need of  updating  
43. tabase values with  those in the Contact object     Deletes from the database the Con   tact object with Contactld equal to  mContactId of the object  Contact  object   s values are re initialized to  their defaults     Shared method that deletes the  Contact object from the database  with a ContactId value equal to the  ContactId argument of the method     Shared method that returns a  SqlDataReaderobject populated  with a list of contacts whose  surname   s first letter matches the  FirstLetterOfSurname argu   ment This argument is optional  if  left off  all Contact objects regard   less of surname   s first letter are  included in the DataSet   s rows     Table continued on following page    9    Chapter 1    Method Return Type    GetContactsByFirstLetterAsCollection ByVal SqlDataReader  DiaryId As Integer  Optional ByVal  FirstLetterOfSurname As Char     Finally  the Contact class contains the following properties     Property Type Description    Description    Shared method   that returns a  ContactCollection  object populated   with Contact objects  whose surname   s first  letter matches the  FirstLetterOfSurname  argument  This argument  is optional  if left off  all  Contact objects regard   less of surname   s first  letter are included in   the DataSet   s rows     ContactId Long Each contact is represented by a unique ID  The ID is auto   generated by the Contact table in the database whenever a    new contact is inserted        FirstName String Contact   s first
44. thing to do with contacts  including the storing and retrieving of con   tact information in the database     The Online Diary and Organizer    It has two constructors  outlined in the following table     Constructor    New ByVal Diaryid as Integer     New  ByVal ContactId As Long     Description    Creates a new Contact object with all properties set to    their default values     Creates a new Contact object with its properties retrieved  from the database using the argument ContactId     Having created a Contact object  saving it involves simply calling the Save    method  The class will  work out whether it   s a new contact that needs to be inserted into the database  or an existing one that  needs to be updated  In addition to the Save    method  the Contacts class contains two Delete     methods  as well as two GetContacts    methods  all of which are outlined in the following table     Method    Save       DeleteContact       DeleteContact  ByVal  ContactId As Long     GetContactsByFirstLetter  ByVal  Diaryld As   Integer Optional ByVal  FirstLetterOfSurname   As Char     Return Type    None    None    None    SqlDataReader    Description    Saves a fully populated Contact  object  If it   s a new contact  Save     calls InsertNewContact sub  and  the details are inserted into the  database  The new Contactld is  returned from the database and  entered into mContactId  If the con   tact already exists in the database   Save   calls UpdateContact  which  updates the da
45. ude extra functionality such as personalizing the user experience or providing different  levels of membership  admin  user  operator   among other things     aspnet_Users   Column Name Data Type Allow Nulls  E  ApplicationId uniqueidentifier    UserId uniqueidentifier  zi UserName nvarchar 256      LoweredUserName nvarchar 256     a MobileAlias nyarchar 16     Ei Is  nonymous bit  _  LastactivityDate datetime     l    0ogagaaoonon       Figure 1 11    The tables of the main Online Diary database and their roles are listed in the following table     Table Name Description   Diary Contains details of all Online Diary users  their DiaryId  and names   DiaryEntry Contains all the diary entries for all diary users    DiaryEvent Contains all the diary events for all diary users    Contact Holds the details of all contacts for the diaries     The key that links all of the tables together is the Diaryld field  It   s the primary key field in the Diary  table and a foreign key field in all the other tables  Why not use the UserName field  Basically speed      it   s easier and therefore faster for the database to do joins and searches on an integer field than it is on  character based fields     All access to the database is via a stored procedure  The naming convention is simply as follows     ActionThingThisActionRelatesTo    Chapter 1    Consider this very simple stored procedure   DeleteContact  Rather unsurprisingly  DeleteContact deletes a contact from the database  The naming 
46. up web applications created on one  machine onto another  To install the application on your PC  simply copy the entire directory and files  from the accompanying CD ROM  or download it from www wrox com  onto a directory on your PC   for example  C   Websites   In VWD  all you have to do is choose File Open Web Site and browse to  the folder where you copied the files  Then press F5 to run it     34    The Online Diary and Organizer    Alternatively  if you have IIS installed make the OnlineDiary directory you copied over a virtual direc   tory and then simply browse to SignOn aspx     To find out how to modify the Online Diary application  visit www  wrox   com and download this chapter   s  code  or you can grab it from the companion CD ROM in the back of the book     Summary    In this chapter you ve seen how to create a fully functioning diary and contacts management system  all  with only a little code thanks to ASP NET 2 0   s new controls and functionality  The new security controls  in particular help save a lot of time and coding  In this chapter they   ve been used to create users and  login control  However  they can also help provide a lot more functionality like creating different types  of user roles  which then allows you to specify what users can and cannot do based on their role  Or you  can let users determine the look and feel of their pages using their account details and ASP NET 2 0   s  new login and role controls     Another great control you discovere
47. xamined next     Above the data access layer is the business layer providing all the rules and intelligence of the system   The business layer has been coded as seven classes  which you tour through shortly     Finally  the bit the user sees is the presentation layer  consisting of a number of  aspx files that utilize the  business and data access layers to create the diary   s interface  This layer is discussed in the last part of  this section     The Data Access Layer    The Online Diary uses a SQL Server 2005 Express database  However  there   s no reason why this couldn   t  be changed to work with other databases  If the database supports stored procedures  then in theory all  that   s needed is a change of connection string and creation of stored procedures matching those in the  current SQL Server database  If the database doesn   t support stored procedures     for example  MS  Access     changes to class code would be necessary but not difficult     Figure 1 10 shows the tables in the Online Diary database  DiaryDB                                                               DiaryEvent  Diary   Column Name Data Type Allow Nils  z Column Name   Data Type Allow Nuls _   7 E  Eventid bigi Lm  E  DiaryId ik D ad DiaryId int m   E  UserName nvarchar 256  m J EventName nvarchar 50   mj  FirstName nvarchar 50  Vv f  EvertDescription nvarchar 250  F  J LastName nvarchar 50  d  E  EvertDate datetime  m  m  E  EvertDuration int  m   J    DiaryEntry Contact  mf Column Name   
    
Download Pdf Manuals
 
 
    
Related Search
    
Related Contents
資料2 消費者安全法に基づく事故情報通知制度等について  Istruzioni per l'uso  User Manual - CD Net Plus SRL  Инструкция - Felisatti  Clear2O CWS100A User's Manual  Phoenix Contact CLIPLINE Control Cabinet Technology  運動施設 - 四日市市  Modèle ERSAD II    Copyright © All rights reserved. 
   Failed to retrieve file