30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20 ASP .<strong>NET</strong>, Web Forms and Web Controls 1011<br />

Fig. 20.39 Code-behind file for the login page for authors application (part 5 of 5).<br />

In Fig. 20.39, the Page_Init event handler is defined in lines 42–77. If the page is<br />

being loaded for the first time, lines 50–76 execute. The database code is contained within<br />

a Try/Catch/Finally block (lines 53–74) <strong>to</strong> handle any database connectivity exceptions<br />

and <strong>to</strong> ensure that the database is closed. Lines 58–59 execute the SQL query that<br />

<strong>Visual</strong> Studio generates at design time—this query simply retrieves all the rows from the<br />

Authors table of the Books database. Lines 63–65 iterate through the rows, placing the<br />

item in the first column of each row (the author name) in<strong>to</strong> nameList.<br />

The reader might notice that we use an OleDbDataReader, an object that reads data<br />

from a database. We did not use an object of this type before, because the OleDbDataReader<br />

is not as flexible as other readers we discussed in Chapter 19. The object can read<br />

data, but cannot update it. <strong>How</strong>ever, we use OleDbDataReader in this example because<br />

we need only read the authors’ names; this object provides a fast and simple way <strong>to</strong> do so.<br />

In this example, we use a Cus<strong>to</strong>mValida<strong>to</strong>r <strong>to</strong> validate the user’s password. We<br />

define a handler (lines 80–124) for the ServerValidate event of the Cus<strong>to</strong>mValida<strong>to</strong>r,<br />

which executes every time the user clicks Submit. This event handler contains<br />

a ServerValidateEventArgs parameter called args. The object referenced by<br />

args has two important properties: Value, which contains the value of the control that<br />

the Cus<strong>to</strong>mValida<strong>to</strong>r is validating, and IsValid, which contains a Boolean representing<br />

the validation result. Once the event handler completes, if IsValid is True,<br />

the HTML form is submitted <strong>to</strong> the Web server; if IsValid is False, the Cus<strong>to</strong>m-<br />

Valida<strong>to</strong>r’s ErrorMessage is displayed, and the HTML form is not submitted <strong>to</strong><br />

the Web server.<br />

To create and attach an event handler for the ServerValidate event, double-click<br />

Cus<strong>to</strong>mValida<strong>to</strong>r. The definition for this event handler (lines 80–124) tests the<br />

selected user name against the password provided by the user. If they match, the user is

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!