Creating the Tour Diary Page


We're going to get really lazy here and reuse most of the GuestBook.aspx page's functionality, which we created in the previous chapter. Using the Data window, create a new table in the CAM database, and call it TourDiaryEntries. Use the following columns:

Column Name

Data Type

Property

Value

TourDiaryEntryID

Int

Required:

Yes

Primary Key:

Yes

Auto-increment:

Yes

First value:

1

Increment:

1

Author

VarChar

Required:

Yes

Field Size:

50

Subject

VarChar

Required:

Yes

Field Size:

100

Message

Text

Required:

No

PostedDate

DateTime

Required:

No

Default:

getdate()

The TourDiaryEntries table is structured exactly the same as the GuestBookEntries table that we created in the previous chapter.

Try It Out—Creating the Tour Diary

Ever feel like wanting to cut corners on an assignment? I do, right now, so here's what we're going to do:

  1. First of all, you need to copy GuestBook.aspx from the previous chapter into a new file called TourDiary.aspx in the Secure directory. The easiest way to do this is using Windows Explorer to manipulate the files as you would with any other files.

  2. Open TourDiary.aspx and display the All view.

  3. Remove the Register directive at the top of the page for the menu. We're not going to use the Menu user control on this page since we're not using that look and feel for our secure site. We're going with the "barren" look!

  4. Next, we need to change the Page_Load subroutine. Instead of asking the user for their name, we're going to display their name based on their login. So here, we'll set the new label that will display the user's name:

    Sub Page_Load(Sender As Object, E As EventArgs)   If Not Page.IsPostBack Then     ' Databind the data grid on the first request only     ' (on postback, rebind only in paging and sorting commands)  lblAuthor.text = User.Identity.Name     BindGrid()   End If End Sub 

  5. Edit BindGrid() to read records from the TourDiaryEntries table instead of GuestBookEntries:

    Sub BindGrid()   Dim CommandText As String   ' TODO: update the CommandText value for your application   If SortField = String.Empty Then  CommandText = "select Author, Subject, Message, PostedDate" & _  " from TourDiaryEntries order by PostedDate desc"   Else  CommandText = "select Author, Subject, Message, PostedDate" & _  " from TourDiaryEntries order by " & SortField   End If

  6. Rename the InsertGuestBookEntry() method to InsertTourDiaryEntry() and modify the query string to insert into the correct table:

     Function InsertTourDiaryEntry(ByVal author As String, ByVal subject _  As String, ByVal message As String) As Integer   Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("cam"))  Dim queryString As String = _  "INSERT INTO TourDiaryEntries (Author, Subject, Message)" & _  " VALUES (@Author, @Subject, @Message)"   Dim cmd As New SqlCommand(queryString, conn)   Dim rowsAffected As Integer = 0

  7. Modify btnOK_Click() to call InsertTourDiaryEntry(). Also, instead of passing txtAuthor.text as the author name, pass User.Identity.Name:

    Sub btnOK_Click(sender As Object, e As EventArgs)   Dim NumRecords As Integer = 0   If IsValid Then  NumRecords = InsertTourDiaryEntry( _  User.Identity.Name, txtSubject.text, txtMessage.text) 

    Since we require the band members to log in, we don't need them to enter their name each time they post a message. Instead, we're using the intrinsic User object to determine who they are and we store their name from the User object in the database.

  8. Change the status message we display in btnOK_Click when the entry is created successfully:

        Else  lblStatusMsg.text = "Entry created successfully."       InitPostForm     End If 

    Take out the "Thanks for letting us know what you think." It just doesn't seem appropriate when you're talking to the other band members!

  9. Next, you need to change the InitPostForm() subroutine. We change txtAuthor to a label in the code later, so we don't need to initialize it after a message is posted. Simply delete the following line:

      txtAuthor.text = ""
  10. Change the top of the HTML as shown here. We're removing the style sheet reference and the menu control:

      <uc0:Menu  title="Tour Diary" runat="server"></uc0:Menu> 

  11. Change the textbox where we asked the user for their name to a label, and remove the RequiredFieldValidator for it:

      <asp:Label  runat="server" MaxLength="50"></asp:Label> 

  12. We're utilizing CAM.dll on this page. As mentioned in the previous chapter, any DLLs that you utilize in your pages must be placed in a directory called bin. Since we had to configure the Secure directory to be a new IIS application in order to set up security, we'll also need to create a bin directory within this directory, and place the CAM.dll file in there. It'd be nice to utilize the DLL in one place, but it makes sense from a security standpoint. This way, one application on a server can't maliciously make calls into DLLs on other applications. This is especially important for web hosting companies. So, create a bin directory within the Secure directory and copy CAM.dll to it.

    Important

    A bin directory must always reside within the root of a web application in order for ASP.NET to locate the DLL, and the classes it contains, correctly.

  13. Now we can test the TourDiary.aspx page to ensure that it works. Here's my first successfully posted entry:

    click to expand

If this page doesn't work for you immediately then there are a couple of things that you need to bear in mind when constructing this functionality.

First of all, you may get a complaint about not being able to connect to the database with which you are working. If you have created a new Web.config file especially for this chapter, then you may have forgotten to add the connection properties with the connection string details:

  <appSettings>  <add key="cam"  value="server=(local)\NetSDK;trusted_connection=true;database=cam"/>  </appSettings> 

Also, you may find that the emoticons don't work. This is because you may have forgotten to have an Images folder within the Secure folder. This is where all the emoticon image files are stored so that the application can find them when a user uses them, as we discovered in the previous chapter.

How It Works

There's not really much to explain here since we're using a copy of the page we created in the previous chapter. We can do this because the data and layout is essentially the same. All we've done is change the name of the SQL statements so that they point to the correct table, and renamed the functions. We've also removed a little of the code and design that's not necessary.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net