Testing and Stabilizing an Application
Questions derived from the 70-547VB – PRO: Designing and Developing Web-Based Applications by Using the Microsoft .NET Framework (VB.NET).
Objective: Testing and Stabilizing an Application
SubObjective: Resolve a bug
Item Number: 70-547VB.5.5.3
Single Answer, Multiple Choice
You are developing a Web application for existing retail customers to order sold-out products online. All customers must be registered in the Web application to place product orders. The Web application provides a registration page for new customers to create a user account.
You create the register.aspx Web page for users to create new or modify existing user accounts. Once users log on to the Web application, their user account information is stored in session state as a UserInfo object.
The register.aspx Web page contains the following code: (Line numbers included for reference only.)
01 Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
02 Dim userInfo As UserInfo = _
CType(Session(“user”), UserInfo)
03 If Not Page.IsPostBack Then
04 txtUsername.Text = userInfo.Username
05 txtPassword.Text = userInfo.Password
06 txtAccountNo.Text = userInfo.AccountNo
07 Else
08 userInfo.Username = txtUsername.Text
09 userInfo.Password = txtPassword.Text
10 userInfo.AccountNo = txtAccountNo.Text
11 End If
12 End Sub
When you load the register.aspx Web page for first time an unhandled exception is thrown. What should you do to load the Web page successfully?
A. Replace line 02 with following code:
If Session(“User”) Is Nothing Then _
Session(“user”) = New UserInfo()
Dim userInfo As UserInfo = _
CType(Session(“user”), UserInfo)
B. Wrap the code in lines 2 – 11 with the following code:
Try
‘Lines 2 – 11
Catch ex as Exception
Response.Redirect(“error.aspx”)
End Try
C. Wrap the code in lines 2 – 11 with the following code:
If Not Session(“User”) Is Nothing
‘Lines 2 – 11
End If
D. Replace line 02 with following code:
Dim userInfo As UserInfo
If Session(“User”) Is Nothing Then
userInfo = New UserInfo()
Else
userInfo= CType(Session(“user”), UserInfo)
End If
Answer:
A. Replace line 02 with following code:
If Session(“User”) Is Nothing Then _
Session(“user”) = New UserInfo()
Dim userInfo As UserInfo = _
CType(Session(“user”), UserInfo)
Tutorial:
To have the Web page load successfully, you should replace line 02 with following code:
If Session(“User”) Is Nothing Then _
Session(“user”) = New UserInfo()
Dim userInfo As UserInfo = _
CType(Session(“user”), UserInfo)
This code verifies the UserInfo object exists in session state and sets the session state accordingly. If the UserInfo object does not exist in session state, then the session state is initialized to a newly instantiated UserInfo object. Then, the userInfo variable is set to the UserInfo object in session state. The UserInfo object will either be the newly instantiated UserInfo object or the existing UserInfo object in session state. This ensures that a NullReferenceException will not be thrown when accessing properties of the UserInfo object and the page will load successfully. It is a recommended practice to always check for the existence of an object before accessing that object’s methods, properties, or events.
You should not use a try…catch block to load the Web page successfully because, though the page will load, it will not perform the required logic in this scenario. Using a try…catch block will only have the Web application fail gracefully, not perform successfully.
You should not wrap the code in lines 2 – 11 with an if statement that verifies the UserInfo object exists in session state because this will not perform the required functionality if the UserInfo object does not exist in session state. Also, it does not include a mechanism to store the new UserInfo values in session state, so the if statement will always return false unless the UserInfo object is stored in session state elsewhere in the Web application.
You should not use the code that verifies the UserInfo object does not exist in session state and instantiates a new UserInfo object, but then stores it in the userInfo variable, because this will not store the new UserInfo object in session state. If the UserInfo object does not exist in session state, then the session state should be initialized to the newly instantiated UserInfo object. Then, the userInfo variable can be set to the UserInfo object in session state.