MCAD/MCSD Training Guide (70-315): Developing and Implementing Web Applications with Visual C# and Visual Studio .NET (First Edition) by Amit Kalani

FAQ

 

Chapter 1

I am attempting to execute the very first example of the book (Step By Step 1.1). However, I am getting the following script runtime error when trying to browse to an ASPX page:
expected ; in line 6 char 9

If you get the above mentioned script runtime error when trying to browse to an ASPX page, the most likely reason is that the ASP.NET file extension are not properly registered with Internet Information Services (IIS). This normally happens if you install IIS after installing the .NET Framework/Visual Studio .NET.

You may be able to fix this problem by registering  the ASP.NET extensions with IIS. To do this, issue the following command

If you are using Visual Studio .NET 2002, run this:
"C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\aspnet_regiis.exe -i"

If you are using Visual Studio .NET 2003, run this:
"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i"


Chapter 4

I get an exception when trying to run Step By Step 4.8. What's the solution?

Exception occurs because ASP.NET do not have access to create necessary registry entries to create an event source. There are a few ways to overcome this problem:

1. Easy (but not recommended) way to make this example work is to modify the processModel element in the machine.config file and change its userName attribute from machine to SYSTEM. The default setting machine enables ASP.NET to run under an unprivileged user account, ASPNET, on Windows XP. Changing it to SYSTEM will cause ASP.NET to run under the context of the high-privileged local system account.

2. A better way is to leave the processModel’s username set to machine and create the required registry entry yourself. Open the Registry editor and access the following key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
Create a new key at this level by the following name: Mileage Efficiency Calculator

Now within this key, add a new string value and set its name to EventMessageFile and set its value to: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\EventLogMessages.dll

Finally, you need to modify the source code for the Application_Error method as shown below:

protected void Application_Error(Object sender, EventArgs e)
{
    // Get the Exception object wrapped in the InnerException property
    Exception unhandledException = (Exception)Server.GetLastError().InnerException;

   // Create an EventLog instance and assign its source.
   EventLog eventLog = new EventLog("Application");
   eventLog.Source = "Mileage Efficiency Calculator";

   // Write an informational entry to the event log.
   eventLog.WriteEntry(unhandledException.Message +
                unhandledException.StackTrace);
   Response.Write("An exception occurred: Created an entry in the Application event log");
   Server.ClearError();
}


3. If you don’t want to modify Windows Registry then just modify the Application_Error method to the following:

protected void Application_Error(Object sender, EventArgs e)
{
    // Get the Exception object wrapped in the InnerException property
   Exception unhandledException = (Exception)Server.GetLastError().InnerException;

    // Create an EventLog instance and assign its source.
   EventLog eventLog = new EventLog("Application");
   eventLog.Source = "Application";

    // Write an informational entry to the event log.
    eventLog.WriteEntry(unhandledException.Message +
               unhandledException.StackTrace);
    Response.Write("An exception occurred: Created an entry in the Application event log");
    Server.ClearError();
}

Instead of creating a separate Event Source, this method will write the event log entries in the existing source named, Application. You will, in this case, receive the following message in the event log preceding your message:

The description for Event ID ( 1000 ) in Source ( Application ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event:

You can just ignore this information or modify registry key as shown in method 2 above to specify the EventMessageFile value,



Chapter 5

Step By Step 5.12 has an annoying feature - after you click on a master record, it populates a child table at the bottom of the screen, but you have to scroll way down to see it. Is there any way to directly navigate to the child table?

Yes, there is a way to achieve this functionality. However, this will require the use of client-side JavaScript code. To make the changes, find this line of code in method ShowDetailGrid() :
this.detailDataGrid.Visible = true;
and, add this new line of code just after it:
RegisterStartupScript("edit","<script language = 'JavaScript'>detailDataGrid.focus();</script>");


On page 386, there is a note that suggests the use of explicit casts over DataBinder.Eval() method for improving performance. However, when I modify Step By Step 5.19 to use explicit cast as follows:
<td><%# String.Format("{0:c}",((DataRowView)Container.DataItem)["Freight "])%></td>
I get a compilation error with the following message:
Compiler Error Message: CS0246: The type or namespace name 'DataRowView' could not be found (are you missing a using directive or an assembly reference?)
How do I resolve this problem?

The reason for this problem is that the compiler cannot resolve the reference to the DataRowView class, which belongs to the System.Data namespace. There are at least two techniques to solve this problem.

Technique 1: Use the fully qualified type name. For example use the following code:
<td><%# String.Format("{0:c}",((System.Data.DataRowView)Container.DataItem)["Freight "])%></td>

Technique 2: Use the @Import directive in your page. For example, include this:
<%@ Import Namespace="System.Data"%>

Now, the compiler will attempt to look for types in the above namespace and you can continue coding like this:
<td><%# String.Format("{0:c}",((DataRowView)Container.DataItem)["Freight"])%></td>

Note that to import multiple namespaces, you need to use multiple @Import directives in your page.