Exam 70-305 Hints and Tips...

This 'article', for want of a better word, is a collection of hints and tips for the 70-305 Microsoft Certification Exam: Developing and implementing web applications with VB.NET and ASP.NET.


By: Chris Sully Date: July 19, 2003

Introduction

This 'article', for want of a better word, is a collection of hints and tips for the 70-305 Microsoft Certification Exam: Developing and implementing web applications with VB.NET and ASP.NET. Each entry addresses areas of knowledge known to be tested during versions of the exam but that are concerned with knowledge a little more obscure than most areas tested. The issues are presented in the following categories: ADO.NET, Configuration, Miscellaneous (caching, debugging, code behind, cultures, etc.).

ADO.NET

AcceptChanges and GetChanges

DataTable.AcceptChanges() accepts any changes to the local data model.

GetChanges method on a dataset returns the changes made to the original dataset since being loaded.

Connection String
(e.g. SqlConnection):

Normally 3 constituent parts:

Data source:name of the server
Initial Catalog:name of database
Authentication information:either 'Integrated Security=SSPI', or 'User ID=username; Password=password'

e.g. A SqlConnection connection string to database PersonnelReports in the instance of SQL Server named Reporting on server Prod01 using integrated security would be:

"Data Source= Prod01\Reporting;Initial Catalog=Reporting; Integrated Security=SSPI"

Note that for an OleDb connection you also need the 'Provider' string, i.e. 'Provider=SQLOLEDB;'

There are additional parameters but these are the minimum possible.

ContinueUpdateOnError

SqlDataAdapter object's ContinueUpdateOnError property will cause the DataAdapter to do just that when set to true: if errors are encountered when updating the database from the local data model.

Database Connection pooling

Is configurable when you open the connection to the database, i.e. via the Min Pool Size (default 0) and max pool size properties (default 100).

DataSet.Merge

Overloaded as follows:

arg1: dataset, datatable, datarow (array)

arg2: Boolean: merges a specified DataSet and its schema into the current DataSet, preserving or discarding any changes in this DataSet according to the given argument.

arg3: MissingSchemaAction: handles an incompatible schema according to the given argument: add, addwithkey, error, ignore.

Datasets, DataAdapters and Schema

The Fill method of the DataAdapter fills a DataSet only with table columns and rows from a data source; though constraints are commonly set by the data source, the Fill method does not add this schema information to the DataSet by default. To populate a DataSet with existing primary key constraint information from a data source, you can either call the FillSchema method of the DataAdapter, or set the MissingSchemaAction property of the DataAdapter to AddWithKey before calling Fill. This will ensure that primary key constraints in the DataSet reflect those at the data source. Foreign key constraint information is not included and will need to be created explicitly, as shown in Adding Constraints to a Table.

DataTable.KeyDeleteRule

By default the KeyDeleteRule is set to Cascading delete, i.e. if a ForeignKeyConstraint has been established between two tables and you delete a row from the primary key table the row in the child table will be deleted as well. To disable, this should be set to null. However, you may then get errors when you perform deletes as the constraint may then be violated.

SQLServer 6.5

SqlConnection objects don’t work with SQL Server 6.5–an OleDbConnection must be used instead.

Searching/ returning XML

For the quickest searching and querying of XML data use the XPathDocument object and related methods.

Transactions

Use the BeginTransaction method of the OleDbConnection object to create an OleDbTransaction object. Assign the OleDbTransaction object to the Transaction property of your OleDbCommand object. If an error occurs, use the OleDbTransaction object to roll back the changes. The same applies to the SQL specific versions of these objects.

Configuration

Configuration Overriding

By default, configuration files located in subdirectories override and extend all configuration settings defined in parent configuration files. In application hosting scenarios, administrators often want to lock or make some settings on a site unchangeable to prevent modification. For example, an administrator might want to lock the sandbox security settings for hosted applications so that Web users cannot attack the system.

Administrators can lock configuration settings by adding an allowOverride="false" attribute to a <location> directive. This tells the configuration system to throw an error if a lower-level configuration file attempts to override any configuration section defined within this locked <location> directive.

The following configuration file example (which could be stored at either the main system level or at the site level) locks the application identity of two different ASP.NET applications (application1 and application2) and configures the applications to run only under specific accounts.

<configuration >
  <location path="application1" allowOverride="false" >
    <system.web >
      <identity impersonate="true" userName="application1" password="pwd1"/ >
    </system.web >
  </location >
  <location path="application2" allowOverride="false" >
    <system.web >
      <identity impersonate="true" userName="application2" password="pwd2"/ >
    </system.web >
  </location >
</configuration >

Authentication

The mode is set to one of the authentication methods: Windows, Forms, Passport, or None. The default is Windows. The authentication mode cannot be set at a level below the application root directory. As is the case with other ASP.NET modules, subdirectories in the URI space inherit authentication modules unless explicitly overridden.

e.g. for application root application:

1 application/web.config:

1  <authentication mode="Windows"/ >
2    <authorization >
3      <allow roles="Pupils, Staff"/ >
4      <deny users="*"/ >
5    </authorization >

2 application/reports/web.config:

1  <authentication mode="Windows"/ >
2    <authorization >
3      <allow roles="Staff”/ >
4      <deny users ="*"/>
5    </authorization>

2 is not allowed. You will receive a error message including

"An error occurred during the processing of a configuration file required to service this request."

Line 1 in 2 needs to be removed.

WindowsPrincipal and WindowsIdentity

These two classes are concerned with role based authorization

WindowsIdentity represents the account of the user who is running the current code. WindowsPrincipal can be used to get or set the identity and properties of a user account under which to run the code.

Authorisation

<authorization >
  <deny users="tania, megan" >
  <deny users="?" >
  <allow users="*" >
</authorization >

will achieve configure the application to use the following authorization rules:

Though <allow users="*" > is redundant it might be thrown into the mix to confuse.

Thus, somewhat confusingly, the security works from the top down.

PrincipalPermission

The following example creates two PrincipalPermission objects representing two different administrative users, forms the union of the two, and makes a demand. Demand will succeed only if the active implementation of IPrincipal represents either user Geoff in the role of Manager or user Alex in the role of Supervisor.

Dim id1 As String = "Geoff"
Dim role1 As String = "Manager"
Dim PrincipalPerm1 As New PrincipalPermission(id1, role1)

Dim id2 As String = "Alex"
Dim role2 As String = "Supervisor"
Dim PrincipalPerm2 As New PrincipalPermission(id2, role2)

PrincipalPerm1.Union(PrincipalPerm2).Demand()

Tracing

The .axd file will normally be served from the root application directory. Note that if the directory is a virtual directory this will be a new application. Thus the following will work if 'MyApplication' is a virtual directory.

http://Server1/MyProject/MyApplication/trace.axd

Miscellaneous

Accessing a property of a property

Accessing property of a property is achieved via the syntax <property >- <property >.

e.g.

Private _formatter As MyFormat = New MyFormat()
Public ReadOnly Property Format As MyFormat
  Get
    Return _formatter
  End Get
End Property

'assume control is registered properly as myCtrl:Message

<myCtrl:Message Format-Color="pink" Format-Size="16" runat="server"/ >

CurrentUICulture vs CurrentCulture

CurrentUICulture is used when selecting resources for presentation.
CurrentCulture is used for formatting strings etc.

Code Behind

Example You are maintaining an ASP.NET application named Reporting. The application is written in Visual Basic .NET. The application includes a page named FirstQuarter.aspx that resides within the Reports namespace. The page class is named FirstQuarter. You discover that another developer inadvertently deleted the Page directive for FirstQuarter.aspx. You want to create a new Page directive to allow FirstQuarter.aspx to work properly.

<%@ Page Language="vb" Codebehind="FirstQuarter.aspx.vb" Inherits="Reports.FirstQuarter"% >

Debugging Clientside code:

In Internet Explorer, perform the actions that cause the client-side code to run.

Note, you don’t need to:

This is not required if you are debugging programs by starting them from within the VS.NET environment. It is if you are attaching to an already running process.

EnableViewStateMac

Indicates that ASP.NET should run a machine authentication check (MAC) on the page's view state when the page is posted back from the client. True if view state should be MAC checked; otherwise, false. The default is false.

Note: A view state MAC is an encrypted version of the hidden variable that a page's view state is persisted to when sent to the browser. When you set this attribute to true, the encrypted view state is checked to verify that it has not been tampered with on the client.

SmartNavigation

Set the SmartNavigation attribute to true in the @ Page directive in the .aspx file. When the page is requested, the dynamically generated class sets this property.

When a page is requested by an Internet Explorer 5 browser, or later, smart navigation enhances the user's experience of the page by performing the following:

Smart navigation is best used with ASP.NET pages that require frequent postbacks but with visual content that does not change dramatically on return. Consider this carefully when deciding whether to set this property to true.

Throwing an exception

Syntax:

Catch(ex as OleDbException)
  throw(new Exception("message", ex))
End Try

Thus you can re-throw an exception, with the descriptive string being an optional parameter.

Validation Controls

Use the ErrorMessage property to specify the text to display in the validation control when validation fails. This text is also included in the ValidationSummary control, if one is placed on the Web page.

It is possible to display different text for the validation control and the ValidationSummary control. Use the Text property to specify the text to display in the validation control.

Note: If the Text property is set, that text will override the text specified in the ErrorMessage property and appear in the validation control. However, the text specified by the ErrorMessage property always appears in the ValidationSummary control.

VaryByParam and VaryByControl

VaryByParam

A semicolon-separated list of strings used to vary the output cache. By default, these strings correspond to a query string value sent with GET method attributes, or a parameter sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the requested document for each specified parameter. Possible values include none, *, and any valid query string or POST parameter name.

Important: This attribute is required when you output cache ASP.NET pages. It is required for user controls as well unless you have included a VaryByControl attribute in the control's @OutputCache directive. A parser error occurs if you fail to include it. If you do not want to specify a parameter to vary cached content, set the value to none. If you want to vary the output cache by all parameter values, set the attribute to *.

VaryByControl

A semicolon-separated list of strings used to vary the output cache. These strings represent fully qualified names of properties on a user control. When this attribute is used for a user control, the user control output is varied to the cache for each specified user control property.

Note: This attribute is required in a user control @ OutputCache directive unless you have included a VaryByParam attribute. This attribute is not supported for @ OutputCache directives in ASP.NET pages.

e.g.

<%@ OutputCache Duration="10" VaryByControl="State;Country" % >

WebControl.MergeStyle and ApplyStyle Methods

WebControl.MergeStyle Method
Copies any nonblank elements of the specified style to the Web control, but will not overwrite any existing style elements of the control. This method is used primarily by control developers.

WebControl.ApplyStyle Method
Copies any nonblank elements of the specified style to the Web control, overwriting any existing style elements of the control. This method is primarily used by control developers.

References

ASP.NET: Tips, Tutorial and Code
Scott Mitchell et al.
Sams

Developing and implementing web applications with VB.Net and VS.Net
Mike Gunderloy
Que

.NET Framework SDK

Various Online resources