Creating our Application

Chapter 12 - Writing an Application
byJohn Kauffman, Fabio Claudio Ferracchiatiet al.?
Wrox Press ?2002

Having run the setup routine, you've got a running application under the URL http://localhost/bid. At this stage, you've got two options:

  1. You can open the downloaded solution in Visual Studio .NET (by picking Open Solution from the File menu, or by double-clicking the Bid.sln file in Windows Explorer). This gives you a complete working set of code that you can examine, using the remainder of this chapter for explanations.

  2. You can create a new project in Visual Studio .NET and use the code listings in the remainder of this chapter to build the application for yourself. This involves more work, but it does give you more experience of using Visual Studio .NET.

Creating a New Application

In Visual Studio .NET, open the File | New | Project dialog, and ensure that you have Visual Basic Projects selected as your Project Type. Then, click on ASP.NET Web Application as your Template, and enter http://localhost/MyBid in the Location box. We've had to pick a new name here, because the downloaded application uses the name Bid.

When you click OK, Visual Studio .NET will create the project, and after that our project is ready to be coded. All we have to do now is write the code!

click to expand

If the Solution Explorer showing these files is not visible, you can press Ctrl+Alt+L, select View Solution Explorer, or click the Solution Explorer button on your VS.NET toolbar.

Preloading Code

Since we aren't actually going to code everything in this application from scratch, we need to copy some of the code from the downloaded sample. First, you need to create a directory in which to store the components. In the Solution Explorer, right-click on MyBid, choose Add from the context menu, and then select New Folder:

click to expand

As a result of this operation, the new folder is created directly in the Solution Explorer. Just make sure that you change its name to Components.

Next, we need to import the component files from the existing application. Select the Components directory, right mouse click and select Add again, but this time pick Add Existing Item. From the next dialog, navigate to the directory where you installed the sample, and select all four files from the Components directory there. Click the Open button, and these files will be copied into our new project.

We've mentioned two of these files before - they contain the DAL components that provide the interface between our application and the database. The file with the .resx suffix is a resource file used by VS.NET, while the last one is Tools.vb, which contains the following:

    Imports System.Web    Public Class Tools      Public Shared Function IsLoggedIn() As Boolean        Dim ctx As HttpContext = HttpContext.Current        If ctx.Request.Cookies("email") Is Nothing OrElse _           ctx.Request.Cookies("email").Value = "" Then          Return False        Else          Return True        End If      End Function    End Class 

This class just provides a central function to see whether a user is logged in or not. It accesses the current HTTP context - you don't need to worry especially about this, but it's required if you need to access the HTTP objects from within a component. From the context, we access the Request object, and from there the Cookies collection, and the cookie called email. This cookie contains the e-mail address of a logged-in user. If it's empty, the user isn't logged in.

Setting the Configuration

Now that the components are installed, we need to set the configuration details. For this application, this simply consists of the information required to connect to the database, which we can store in the web.config file. Just add the following between the <configuration> and <system.web> elements, save the file, and close it:

    <appSettings>      <add key="ConnectionString"           value="server=(local)\NetSDK;database=bids;Trusted_Connection=true" />    </appSettings> 



Beginning ASP. NET 2.0 and Databases
Beginning ASP.NET 2.0 and Databases (Wrox Beginning Guides)
ISBN: 0471781347
EAN: 2147483647
Year: 2004
Pages: 263

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