ASP.NET 2.0: Learning the Basics, Part III

Page Load Event

Page Load event is fired when the page is loaded and accessed for the first time and when it is loaded in response to a client postback.  But if you need to initialize variables or want something to happen only once during the first load of the page, you can use the IsPostBack property of the Page object to determine if the page is loaded for the first time or due to postback:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ' do some initialization here...
        End If
    End Sub

Application and Session Events

If you need to code on the application and/or session events, add the Global.asax file (Add New Item… and select Global Application Class).  You will find 3 application events (Start, End, and Error) and 2 session events (Start and End).

Application and Session State

As always, you can save your application and session states using the Application and Session objects respectively:

Application("MyAppVariable") = "MyAppValue"

and

Session("MySessionVariable") = "MySessionValue"

Session Identifiers

To identify each browser session, ASP.NET assigns a unique ID to a session, the SessionID property of the Session object.

ViewState Object

ViewState object is useful in persisting data on the page between postbacks.  It makes use of the __VIEWSTATE input hidden field that is inserted into the generated page.  __VIEWSTATE contains the state of all the controls on the page in encrypted form.

Profile Object

New in ASP.NET 2.0 is the Profile object.  This is now the preferred way to store user information across multiple visits to a web application.  It’s like the Session object but better.  It is strongly typed and you have the option to save it in the database, in an XML file, or in memory. 

To add properties to your Profile object, you need to add them to your web.config file, inside the <system.web> element:

<configuration>
   <appSettings/>
   <connectionStrings/>
   <system.web>

      ...

      <anonymousIdentification enabled="true" />
      <profile>
         <properties>
            <add name="MyProfileVariable" allowAnonymous="true" />
         </properties>
      </profile>
    
  </system.web>
</configuration>

In your code you can access or set the property like this:

    Profile.MyProfileVariable = "MyValue"

Other ASP.NET Objects

Other objects that are also available in ASP.NET are the following: Request, Form, Trace, User, Server, and PreviousPage.

Query String

To pass data from one page to another, you can use the name/value pair in the query string of the URL:

MyWebPage.aspx?MyVariable=MyValue

On the target page, you can access the value in the query string using the Request object:

Request.QueryString("MyVariable")

Cross Page PostBack

New in ASP.NET 2.0 is the ability to postback to a different page.  To use this, set the PostBackUrl property of the Button control to the page that you want to postback to.  To get information from the source page, you can use the PreviousPage object or the Form dictionary of the Request object.  If you want to check if the target page is running as a result of a cross page postback, use the IsCrossPagePostBack property of the PreviousPage object.  Do not use the target page’s IsPostBack property as it is set to false.

ASP.NET 2.0: Learning the Basics, Part II

Validation Controls

You also have the ability to put some validation onto your controls using some of the available validation controls.

RequiredFieldValidator will check if the control you assigned to it (ControlToValidate) has a value.  Other properties you can set are Text, and ErrorMessageText is displayed when no value is entered for the control while ErrorMessage will show up on the ValidationSummary control if you have it setup on your webpage.

CompareValidator will compare the value of the control you assigned to it (ControlToValidate) to some value you specify (ValueToCompare) given the comparison operator you indicated (Operator).  You can also compare it to another control (ControlToCompare).  You also need to set the Type to indicate the type of the values that you want to compare (e.g. String, Integer, Double, Date, or Currency).  As with the RequiredFieldValidator, you can also set the Text and ErrorMessage properties.

Layout Toolbar

If you want to align your controls or lay them out in relation to the other controls, you can make use of the Layout toolbar.  Go to View -> Toolbars and select Layout.  You can align lefts, centers, rights, tops, middles, and bottoms, make same width, height, and size, increase/decrease/remove horizontal/vertical spacing, and bring to front/send to back.

AutoPostBack

If you need to postback to the server after selection to the control is changed without having the user to initiate the postback (using a Button control), you can set the AutoPostBack property to True.

SQL Server 2005: Understanding decimal, numeric, float, and real data types

Since I came from a VB background, I get confused when I encounter these data types in SQL Server.  In VB if I need to use a variable that can hold a number with digits to the right of the decimal point, I declare the variable as single or double.  In SQL Server, you have decimal, numeric, float and real to choose from.  So what is the difference between them?

Decimal and numeric

Decimal and numeric are called exact numerics in SQL Server because they have fixed precision and scale

Precision is the maximum total number of digits that can be stored, both to the left and to the right of the decimal point.  The value for precision can be from 1 to 38.

Scale is the maximum number of digits that can be stored to the right of the decimal point.  The value for scale can be from 0 to precision.

If you don’t specify the precision and scale when you declare the variable as decimal or numeric, by default it will have a precision of 18 digits and a scale of 0 digit.  It’s like declaring it as:

decimal (18, 0)

Since decimal and numeric are both the same, either numeric or decimal will do.

If you are concerned with storage, then use a precision of 9 digits or less and this will take only 5 bytes, the minimum for a decimal or numeric.  A precision of 10-19 digits will give you 9 bytes; 20-28 digits will give you 13 bytes; and 29-38 digits will give you 17 bytes.

Float and real

Float and real are called approximate numerics and are used for floating point numeric data (whatever that means).  Since decimal or numeric will serve my purpose if all I want is a number that can store digits to the right of the decimal point, then I don’t need to bother with float and real unless I am working on a mathematical application.

SQL Server 2005: JOINS and UNION

There are actually 4 joins in SQL Server: INNER, OUTER, CROSS, AND FULL.  If you come from an Access background, you already know INNER, OUTER, and CROSS joins.

INNER JOIN is the default join in SQL Server so any SQL statements that has only JOIN in it is, by default, an INNER JOIN.  Use this type of join if you only want to include records where the joined fields from both tables are equal.  Example of an INNER JOIN:

SELECT *
FROM Products
INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID

OUTER JOIN can be a LEFT OUTER JOIN or a RIGHT OUTER JOIN.  Use LEFT OUTER JOIN if you want to include all records from the table left of the JOIN keyword, and only those records from the table right of the JOIN keyword, where the joined fields are equal  Use RIGHT OUTER JOIN if you want to include all records from the table right of the JOIN keyword, and only those records from the table left of the JOIN keyword, where the joined fields are equal.

When mixing INNER and OUTER JOINS, it is best to do the INNER JOINS first before the OUTER JOINS.

CROSS JOIN is just the cartesian product of all the records on both tables.  It doesn’t have the ON operator like the other 3 JOINS.  Typically used in scientific environments but you can also use it to build your test data.  Example of a CROSS JOIN:

SELECT Customers.CustomerName, Addresses.Address
FROM Customers
CROSS JOIN Addresses

FULL JOIN, also known as FULL OUTER JOIN, is like having a LEFT OUTER JOIN and a RIGHT OUTER JOIN in one JOIN.  It is rarely used which might be the reason Access does not have this type of join.

UNION, unlike the JOINS, appends the result from your second query to your first query, essentially adding more rows instead of adding more columns.  When you use UNION, all queries should have the same number of columns.  The data types of each column in a query should be implicitly compatible with the data type in the same relative column in the other queries.  The heading returned for a UNION query is taken from the first query.  This means you only need to provide meaningful column names on your first query.  Also by default the UNION query returns DISTINCT records so if you want it to return duplicate records, then you need to specify UNION ALL:

SELECT Column1, Column2
FROM Table1 

UNION ALL 

SELECT Column1, Column2
FROM Table2