Answers for Day 15

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions


Quiz

1:

What are the three levels of the three-tier application model?

A1:

A UI layer, a business objects layer, and a database layer.

2:

True or false: You compile objects manually because they're specific to one page.

A1:

False. They're compiled because multiple pages will use them.

3:

Write the command-line command to compile the object file MyObject.vb, referencing the System and System.Web namespaces in a subdirectory named bin.

A1:

The command is

 vbc /t:library /out:bin\MyObject.dll /r:System.dll /r:System.Web.dll MyObject.vb 
4:

Which of the following should be placed in a business object: a class implementing a DataGrid, a method that retrieves a connection string from a configuration file, a method that writes to the browser, or a property that describes the number of instances of this object?

A1:

The second and fourth items belong in a business object. The others do not.

5:

True or false: You use Server.CreateObject to create business objects created in the .NET Framework.

A1:

False. Server.CreateObject is for unmanaged COM objects not created in the .NET Framework.

Exercise

Q1:

Modify the User object you created today to use the Database object you created earlier.

A1:

Save the following code as User2.vb:

 1:    Imports System 2:    Imports System.Data 3:    Imports System.Data.OleDb 4: 5:    Namespace TYASPNET 6: 7:       Public Class UserDetails 8:          public FirstName as string 9:          public LastName as string 10:          public UserName as string 11:          public Password as string 12:          public UserID as string 13:       End Class 14: 15:       Public Class User 16:          public function Login(UserName as string, Password _ 17:             as string) as string 18:             dim intId as string = "0" 19: 20:             dim objDatabase as new Database 21:             objDatabase.ConnectionString = "Provider=" & _ 22:                "Microsoft.Jet.OLEDB.4.0;" & _ 23:                "Data Source=C:\ASPNET\data\banking.mdb" 24: 25:             dim objReader as OleDbDataReader 26:             objReader = objDatabase.SelectSQL _ 27:                ("SELECT UserID FROM tblUsers WHERE " & _ 28:                "UserName = '" & UserName & "' AND " & _ 29:                "Password = '" & Password & "'") 30: 31:             do while objReader.Read 32:                return objReader.GetInt32(0).ToString 33:             loop 34:          end function 35: 36:          public function GetDetails(UserID as integer) as _ 37:             UserDetails 38: 39:             dim objDatabase as new Database 40:             objDatabase.ConnectionString = "Provider=" & _ 41:                "Microsoft.Jet.OLEDB.4.0;" & _ 42:                "Data Source=H:\ASPNET\data\banking.mdb" 43: 44:             dim objReader as OleDbDataReader 45:             objReader = objDatabase.SelectSQL _ 46:                ("SELECT FirstName, LastName, UserName, " & _ 47:                "Password FROM tblUsers WHERE UserID = " & _ 48:                UserID) 49: 50:             dim objDetails as new UserDetails 51: 52:             while objReader.Read() 53:                objDetails.FirstName = objReader.GetString(0) 54:                objDetails.LastName = objReader.GetString(1) 55:                objDetails.UserName = objReader.GetString(2) 56:                objDetails.Password = objReader.GetString(3) 57:                objDetails.UserID = UserID.ToString 58:             end while 59:             objReader.Close 60: 61:             return objDetails 62:          end function 63: 64:          public function Update(objDetails as UserDetails, _ 65:             intUserID as integer) as boolean 66:             dim objOldDetails as new UserDetails 67:             objOldDetails = GetDetails(intUserID) 68: 69:             with objDetails 70:                if .FirstName = "" then 71:                   .FirstName = objOldDetails.FirstName 72:                end if 73:                if .LastName = "" then 74:                   .LastName = objOldDetails.LastName 75:                end if 76:                if .Username = "" then 77:                   .UserName = objOldDetails.UserName 78:                end if 79:                if .Password = "" then 80:                   .Password = objOldDetails.Password 81:                end if 82:             end with 83: 84:             dim objDatabase as new Database 85:             objDatabase.ConnectionString = "Provider=" & _ 86:                "Microsoft.Jet.OLEDB.4.0;" & _ 87:                "Data Source=C:\ASPNET\data\banking.mdb" 88: 89:             objDatabase.ExecuteNonQuery("UPDATE tblUsers " & _ 90:                "SET FirstName = '" & objDetails.FirstName & _ 91:                "', LastName = '" & objDetails.LastName & _ 92:                "', UserName = '" & objDetails.UserName & _ 93:                "', [Password] = '" & objDetails.Password & _ 94:                "' WHERE UserID = " & intUserID) 95: 96:             return true 97:          end function 98:       End Class 99: 100:    End Namespace 

Compile this with the following command:

 vbc /t:library /out:TYASPNET.dll /r:System.dll /r:System.Data.Dll User2.vb Database.vb 

The same ASP.NET pages can be used with this new object.


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

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