Tuesday, May 20, 2008

Explain why tracing helps with exception handling.

Tracing allows you to record unusual events while your application is running, without users being aware of it. If an unanticipated exception occurs, your application can write a message to the trace log, which helps you diagnose problems during testing and after deployment.

Describe the purpose of error pages and why they are needed.

Because Web applications run over the Internet, some exceptions occur outside the scope of the application. This means that your application can’t respond directly to these exceptions. These types of exceptions are identified by HTTP response codes, which IIS can respond to by displaying custom error pages listed in your application’s Web.config file.

List two different exception-handling approaches in ASP.NET Web applications.

Exceptions can be handled in exception-handling blocks using the Try, Catch, and Finally keywords in Visual Basic .NET or the try, catch, and finally keywords in Visual C#. They can also be handled using Error event procedures at the Global, Application, or Page levels using the Server object’s GetLastError and ClearError methods.

Explain why exception handling is important to a completed application

When an unhandled exception occurs in an application, the application stops—the user can’t proceed, and any work he or she did immediately prior to the exception is lost. Exception handling provides a way to intercept and correct unusual occurrences that would otherwise cause these problems.

Explain the difference between handling transactions at the data set level and at the database level.

Data sets provide implicit transactions, because changes to the data set aren’t made permanent in the database until you call the Update method. To handle transactions in a data set, process the Update method and check for errors. If errors occur during an update, none of the changes from the data set is made in the database. You can try to correct the error and resubmit the update, or you can roll back the changes to the data set using the RejectChanges method.

Databases provide explicit transactions through the Transaction object. You create a Transaction object from a database connection and then assign that Transaction object to the commands you want to include in the transaction through the command object’s Transaction property. As you perform the commands on the database, you check for errors. If errors occur, you can either try to correct them and resubmit the command, or you can restore the state of the database using the Transaction object’s RollBack method. If no errors occur, you can make the changes permanent by calling the transaction object’s Commit method.

How do you call a stored procedure?

Create a command object, set the object’s CommandText property to the name of the stored procedure, and set the CommandType property to StoredProcedure. To execute the stored procedure, use the command object’s ExecuteNonQuery, ExcecuteScalar, ExecuteReader, or ExecutelXmlReader method. For example, the following code calls the Ten Most Expensive Products stored procedure on the Northwind Traders database:

Visual Basic .NET
' Create a command object to execute.
Dim cmdTopTen As New SqlCommand(connNWind)
' Set command text.
cmdTopTen.CommandText = "Ten Most Expensive Products"
' Set the command properties.
cmdTopTen.CommandType = CommandType.StoredProcedure
' Create a data reader object to get the results.
Dim drdTopTen As SqlDataReader
' Open the connection.
' Excecute the stored procedure.
drdTopTen = cmdTopTen.ExecuteReader()
Visual C#
// Create a connection for NorthWind Trader's database.
SqlConnection connNWind = new SqlConnection("integrated security=SSPI;" +
"data source=(local);initial catalog=Northwind");
// Create a command object to execute.
SqlCommand cmdTopTen = new SqlCommand(connNWind);
cmdTopTen.CommandText = "Ten Most Expensive Products";
// Set the command properties.
cmdTopTen.CommandType = CommandType.StoredProcedure;
// Create a data reader object to get the results.
SqlDataReader drdTopTen;
// Open the connection.
// Excecute the stored procedure.
drdTopTen = cmdTopTen.ExecuteReader();

How do typed data sets differ from untyped data sets, and what are the advantages of typed data sets?

Typed data sets use explicit names and data types for their members, whereas untyped data sets use collections to refer to their members. The following examples show a typed reference vs. an untyped reference to a data item:

Visual Basic .NET
' Typed reference to the Contacts table's HomePhone column.
DataSet1.Contacts.HomePhoneColumn.Caption = "@Home"
' Untyped reference to the Contacts table's HomePhone column.
DataSet1.Tables("Contacts").Columns("HomePhone").Caption = "@Home"
Visual C#
// Typed reference to the Contacts table's HomePhone column.
DataSet1.Contacts.HomePhoneColumn.Caption = "@Home";
// Untyped reference to the Contacts table's HomePhone column.
DataSet1.Tables["Contacts"].Columns["HomePhone"].Caption = "@Home";
Typed data sets do error checking at design time. This error checking helps catch typos and type mismatch errors, which would be detected only at run time with untyped data sets.

What steps would you follow and what objects would you use to quickly find the number of records in a database table?

There are two ways to accomplish this task:

Use a database connection and a command object to execute a SQL command that re-turns the number of rows in the table.

Use a database connection and data adapter object to create a data set for the table, and then get the number rows in the data set.

Tuesday, May 13, 2008

Why can’t you open a new browser window from within server code?

Server code executes on the server, whereas the new window is created on the client. You need to use client-side code to do things that affect the client, such as upload files, display new windows, or navigate back in history.

What is wrong with the following line of code?

Visual Basic .NET
Visual C#
You can’t use the Transfer method with HTML pages. It works only with .aspx pages.

Where would you save the following data items so that they persist between requests to a Web form?

A control created at run time

An object that provides services to all users

User preferences

Save controls created at run time in the Page object’s ViewState.

Save objects that provide services to all users in the Application state.

Save user preferences in SessionState.

How do you call a member of a base class from within a derived class?

To refer to a member of a base class in Visual Basic .NET, use the MyBase keyword. To refer to a member of a base class in Visual C#, use the base keyword.

In Visual C#, how do you declare a method to make it available without having to first instantiate an object from the class?

To create a method that can be called without instantiating an object, declare that method as static.

In Visual Basic .NET, what is the difference between a class module and a code module?

Class modules are instantiated at run time to create objects that provide separate storage for variables and properties in each instance. Code modules do not have instances, so any module-level variables they use are shared among calls to the module’s procedures.

How does the .NET Framework organize its classes?

The .NET Framework uses namespaces to organize its classes.

List the four major differences between Web and Windows applications.

Web forms cannot use the standard Windows controls. Instead, they use server controls, HTML controls, user controls, or custom controls created specially for Web forms.

Web applications are displayed in a browser. Windows applications display their own windows and have more control over how those windows are displayed.

Web forms are instantiated on the server, sent to the browser, and destroyed immediately. Windows forms are instantiated, exist for as long as needed, and are destroyed.

Web applications run on a server and are displayed remotely on clients. Windows applications run on the same machine they are displayed on.

Explain where Visual Studio .NET stores Web application projects.

Web application projects are stored in a new virtual folder for each project. The properties of that virtual folder determine where the files are physically stored. These properties can be viewed in IIS.

Why doesn’t the Visual Studio .NET Code Editor automatically complete the following partial line of code (Visual C# users only)?

int intX = system.math
The namespace System.Math must be capitalized in Visual C# for ­IntelliSense to recognize the namespace.