Understanding Server.Mappath

When using connection strings (let’s say, to a MS Access Database), many times you will see:

server.mappath(“.\data\northwind.mdb”) & “;”

The connection string is expecting an EXACT path, like:

c:\inetpub\wwwroot\data\northwind.mdb

So, in the ‘Server.Mappath’ example above, what this does is to ‘facilitate’ the use of a relative path. It’s based on the relative location of the database, to the ASP.Net page trying to access it. It literally MAPS the path  to :

c:\inetpub\wwwroot\

Calculate Age from Your BirthDate

First, you need a text box called txtBirthDate.Text, a button and a Label called lblShowAge.Text
The code for the button’s click event should read:

Dim CurrentDate as DateTime = DateTime.Today() Dim BirthDate as DateTime ' Makes sure the text input is converted to a Date: BirthDate = CDate(txtBirthDate.text) Dim diff as TimeSpan = CurrentDate.Subtract(BirthDate) ' 'Cint' Converts the output to an Integer (no decimal places) lblShowAge.text=Cint(diff.Days/365)

Get the Last Write Time of File

In order to get the last time a file (like an MS Access database, or any other file) was written, you can do the following:
Use/import the System.IO namespace
then:

Dim sFileWriteTime as File
label1.text=sFileWriteTime.GetLastWriteTime(Server.MapPath (“/path/yourMDB.mdb”))

Listbox Selected Item

Remember – - the selectedindex of listbox items begin with zero (0). Therefore, if an item is selected, the selectedindex will be zero or larger.

If no item is selected, when an event concerning a listbox’s selectedindex is called, then, the selectedindex will be -1.

Therefore, it is recommended, that any event using the selectedindex of an item in a listbox check first to see if it’s zero or larger.

Make Window Fit any Resolution

This will make the current page fill the entire screen, at whatever resolution the client is using. You can put a reference to this in the Body tag, making a function out of it and using the ‘onload’ call, or you can just put it directly in the page:

<script language="Javascript"> UserWidth = window.screen.availWidth; UserHeight = window.screen.availheight; window.resizeTo(UserWidth, UserHeight); window.moveTo(0,0) </script&gt

Make sure, though, that you are not using any fixed-width tables, as, even though the browser window will be full screen, scrolling will still be necessary, if the tables’ widths are wider than the screen.

Importing from MS Access to SQL Server

With Enterprise Manager, included with SQL Server, it makes it very easy to Import MS Access tables directly into SQL Server. However, there is one thing in particular, to which it is worth paying close attention.

If you have an autonumber field in your MS Access table, when you are Importing it into SQL Server, make sure, that when you select the table, you also click on the ‘Transform’ link, over to the right of the table name. Then, click on the ‘Edit SQL’ button. At that point, you will see the ‘Create Table’ script, which will be used to actually create the table in SQL Server. The section for the autonumber field will look something like:

[id] int NOT NULL,

Make sure that before you accept it, you make sure it’s converted to an Identity field like this:

[id] int NOT NULL Identity,

Find Specific Field in DataSet

Sometimes, when we fill DataSet from a Query, we need to get a particular item in one of the DataTable fields for later use. In order to find a particular item, we can use the field name from the database like this, and assign it to a variable:

sOrderNum = Ds.Tables(0).Rows(0)("ordnum")

We’d just need to have created the variable globally, then, we can use that variable (sOrderNum) easily, anywhere in the page now.

DropDownlist with colors and corresponding hex values

<SELECT id="DropDownList1" name="DropDownList1" runat="server"> </SELECT> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim col As FieldInfo For Each col In GetType(KnownColor).GetFields If col.FieldType Is GetType(Drawing.KnownColor) Then DropDownList1.Items.Add(New ListItem(convertToHexColor(System.Drawing.Color.FromName(col.Name)), convertToHexColor(System.Drawing.Color.FromName(col.Name)))) End If Next End If Dim i As Integer For i = 0 To DropDownList1.Items.Count - 1 DropDownList1.Items(i).Attributes.Add("style", "background-color:" + DropDownList1.Items(i).Text) Next End Sub Function convertToHexColor(ByVal c As Color) As String Return "#" + c.ToArgb().ToString("x").Substring(2) End Function