| |
| Build an End-to-End Application |
| Create Registration page |
In this walkthrough, you will create a new
ASP.NET page that will save new user information to the Users
table.
- Select New from the File menu. You will see the New
File dialog box appear.
- Select (General) from the Templates pane.
- Select the ASP.NET Page template.
- Type a file path in the Location box.
- Type NewUser.aspx in the Filename box.
- Select Visual Basic in the Language drop-down box.
- Click OK. The NewUser.aspx file will open in Design
view.
- From the Web Controls tab in the Toolbox, drag three
Label controls, two TextBox controls and a Button
control onto the ASP.NET page.
- Set the Text property of the top Label to UserName:.
- Set the Text property of the middle Label to
UserPassword:.
- Set the Text property of the bottom Label to empty (no
text).
- Set the ID property of the bottom Label to Message.
- Set the ID property of the top TextBox to txtUserName.
- Set the ID property of the bottom TextBox to
txtUserPassword.
- Set the TextMode property of the user password TextBox
to Password.
Note This property gets or sets the behavior mode of the text
box. In this instance, because a password is sensitive information, the
TextBox will automatically mask the user input.
- Select the Code tab.
- From the Code Builders tab in the Toolbox, drag an
INSERT Data Method Code Builder onto the ASP.NET page.
- You will see the Connect to Database dialog box appear.
- Type localhost in the Server box.
- Click the Database drop-down. ASP.NET Web Matrix will connect
to the data source and populate the Database drop-down with
database names. Select the Orders database and click OK.
- You will see step one of the Code Builder Wizard appear; it is
called the Construct INSERT Query dialog box.
- Select Users in the Tables list view.
Note Leave the Column check boxes set to their default
values.
- Click Next.
- You will see step two of the Code Builder Wizard appear; it is
called the Enter A Caption dialog box. Type AddUser into the
method name box.
- Click Finish.
- The Wizard will close and ASP.NET Web Matrix will place a
function called AddUser in Code view. The function accepts a
userName and userPassword as input parameters and returns an
Integer as an output parameter. The Integer value will
represent the total number of affected rows:
Function AddUser(ByVal userName As String, ByVal userPassword As String) As Integer
Dim connectionString As String = _
"server='localhost'; trusted_connection=true; Database='Orders'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = _
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "INSERT INTO [Users] ([UserName], [UserPassword]) " & _
"VALUES (@UserName, @UserPassword)"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = _
New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.Parameters.Add("@UserName", System.Data.SqlDbType.VarChar).Value = _
userName
sqlCommand.Parameters.Add("@UserPassword", System.Data.SqlDbType.VarChar).Value = _
userPassword
Dim rowsAffected As Integer = 0
sqlConnection.Open
Try
rowsAffected = sqlCommand.ExecuteNonQuery
Finally
sqlConnection.Close
End Try
Return rowsAffected
End Function
- Select the Design tab.
- Double-click the Button control. ASP.NET Web Matrix will open
the Code view at the Button1_Click event.
- Add code to the Button Click event so it calls the AddUser
function, passing the TextBox values as UserName and
UserPassword parameters:
Sub Button1_Click(sender As Object, e As EventArgs)
If AddUser(txtUserName.Text, txtUserPassword.Text) > 0
Message.Text = "Success"
Else
Message.Text = "Failure"
End If
End Sub
- Save and run the ASP.NET page.
- Your ASP.NET page will automatically appear in a new instance of your
Web browser.
- Type a new user name in the UserName box.
- Type a user password in the UserPassword box.
- Click the Button.
Note The message Label will contain the new user insert
status.
- Close the Web browser instance.
- Open the Login.aspx file you developed earlier in this
walkthrough. You will provide the user the option of running the
NewUser.aspx page if their login credentials are incorrect.
- Select the Code tab.
- Modify the Login button Click event code so that it provides a
link to the NewUser page in the message Label text:
Msg.Text = "Invalid Credentials: Please try again or " + _
"<a href='newuser.aspx'>register a new user</a>"
- Save and run the ASP.NET page.
- Your ASP.NET page will automatically appear in a new instance of your
Web browser.
- Type an invalid user name in the UserName box.
- Type a user password in the UserPassword box.
- Click the Button.
Note The user now has the option of supplying valid login
credentials or follow the register a new user link.
- Close the Web browser instance.
Next
Step >> |
|