Creating a Message Board with ADO and XML • Chapter 13 |
589 |
Figure 13.15 Continued
"[Email], [IsAdmin], [IsBanned]) VALUES ('" & userName & _ "','" & password & "','" & firstName & "','" & lastName & _ "','" & email & "',0,0)"
DataControl.ExecuteNonQuery(sql)
Return User.Validate(userName, password)
Else
'this username has already been taken
Throw New ArgumentException("The username is already taken") End If
End Function
First, the CreateUser function scans the database to see if the request username already exists. If it does, it throws an ArgumentException. If the username doesn’t exist, it builds a SQL statement to insert a new row into the user table and executes it. Finally it calls the Validate method and returns the result.
The last method to discuss is the Update method.This method updates the database with the current state of the object. See Figure 13.16 for the Update method.
Figure 13.16 The Update Method (User.vb)
Public Sub Update()
Dim sql As String
sql = "UPDATE [Users] SET [Password] = '" & mPassword & _ "', [FirstName] = '" & mFirstName & _
"', [LastName] = '" & mLastName & _ "', [Email] = '" & mEmail & "'"
If Me.IsAdmin = True Then
sql = sql & ", [IsAdmin] = 1"
Else
sql = sql & ", [IsAdmin] = 0" End If
If Me.IsBanned = True Then
sql = sql & ", [IsBanned] = 1"
Else
Continued