MetaPro Systems Inc.

Home Services Software Approach Who We Are
Resources Testimonials Contact Us Products

Visual Studio .Net Tips & Tricks

We are starting a new service called Visual Studio .Net Tips & Tricks. These Tips will be send to all those who request to be on our mailing list. To sign up, please send email to tips@metaprosystems.com. They will also be posted to our website. The Tips will be in the intermediate to advanced range. We will include working code samples which can be downloaded from this website where appropriate. Working knowledge of Visual Basic and Dot Net is assumed.

All downloadable files are in WinZip Format. You will need WinZip or an equivalent program. You can download an evaluation version at www.winzip.com. If you continue to use it, please pay the nominal cost to register it.

New Tips will be added on an occasional basis.

Copyright 2004 by MetaPro Systems Inc.

Visual Studio Dot Net Tips & Tricks #1 - Controls Within Controls

Project Type: ASP.NET Web Application
Code Behind: Visual Basic

The use of user control within a web application can be a convenient way to modularize your code. For example, suppose you want the header or footer to be the same for each page on your web site. By using a user control, you can change the control at any time without having to revisit each page.

You are allowed to have controls within control. If you have more than one child control within a control or on a page, you can access the properties of one child from within another one.

One reason you might want to do this is that you have a control that you want to appear to be more than one page. You do this by making one visible and the other invisible depending on the actions of the user.

In our example, we create a user control that has three child controls. From each of the three you can make each of the other two visible and the current control invisible.

To create each of the user controls add a new item to your project and choose type user control. Design it as you would a web page. We will need four controls. The main control plus three child controls called Top, Middle and Bottom. You will also need a web page in order to run the control.

To place a control on a page or to place a control another control just drag it from solution explorer. When you do this VS.NET will give the control a name. If you wish to change the name, right-click on the control to view properties and change the value of "Id".

In order to expose the properties of the child control, you must add a line like this:

Protected WithEvents uctTop As Top

Substitute uctTop with the "Id" you gave the child control on the parent control. Substitute Top for the class name of the child control itself. You will see it in the first line of the control code:
Public MustInherit Class Top


Here is the code you need to access the properties of one child control from another:


Dim mParent as Control Dim AnyControl As Middle ' Middle is the class name of the other child control
mParentControl = Me.Parent ' this identifies the Parent
AnyControl = mParentControl.FindControl("uctmiddle") ' uctMiddle is the ' id of the other child in the Parent
AnyControl.Visible = True ' make the control visible Me.Visible = False ' makes this child control invisible

Click below to get the complete source code for our example.

Sample Code


Visual Studio Dot Net Tips & Tricks #2 – Inserting Images into a Datagrid

Project Type: ASP.NET Web Application
Code Behind: Visual Basic

For a recent project that was written in ASP.NET I needed to insert an image in a data grid dynamically. The images resided on the disk where the web site was located rather than in a database. The database contained the name, location and dimensions of the images. I search several books and the internet for an example as to how do this but to no avail. I called Microsoft for help. Depending on how you obtained your license, purchasers of Microsoft software have a certain number of free technical support calls. Check with Microsoft for details. Since I had to use one of my calls to get this solution, I am sending this to you so you won’t have to.

This simple example does not read a database. You can easily adapt this to read the information from a database.



Here are the key portions of the code.

Add a data column for the Name of the Image

Dim mdtEvents As
New DataTable()
mdtEvents.Columns.Clear()
mdtEvents.Columns.Add(New DataColumn("Name", GetType(String)))



add two rows to the datatable store the images in a collection for later use

' add id to class to retrieve later

Call mcloImages.clAdd(1, "Cat", "cat.jpg", 139, 200)

dr = mdtEvents.NewRow()
dr(gColumns.DetailName) = "Cat"
'add the row to the datatable
mdtEvents.Rows.Add(dr)
' add id to class to retrieve later
Call mcloImages.clAdd(2, "Dog", "dog.jpg", 200, 200)

dr = mdtEvents.NewRow()
dr(gColumns.DetailName) = "Dog"
'add the row to the datatable
mdtEvents.Rows.Add(dr)


This code goes in the ItemDataBound event of the grid


If itemType = ListItemType.Header Then
        e.Item.Cells(1).Text = "Image"
End If

If ((itemType = ListItemType.Pager) Or _
(itemType = ListItemType.Header) Or _
(itemType = ListItemType.Footer)) Then
        Return
End If

Dim button As LinkButton = _
CType (e.Item.Cells(0).Controls(0), LinkButton)
e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(button, "")
Dim btnimage As ImageButton = e.Item.Cells(1).Controls(1)

Dim i As Short

mMainPath = Request.PhysicalApplicationPath
' mMainPath = Request.Url.ToString
i = dtgAll.PageSize * dtgAll.CurrentPageIndex() e.Item.ItemIndex() + 1

If i > mcloImages.lCount() Then
        btnimage.Visible = False
Else
       If mcloImages.oItem(i).Path = "" Then
                btnimage.Visible = False
        Else
               btnimage.ImageUrl = mMainPath & mcloImages.oItem(i).Path
               btnimage.Height = Unit.Pixel(mcloImages.oItem(i).Height)
               btnimage.Width = Unit.Pixel(mcloImages.oItem(i).Width)
       End If
End If

Click below to get the complete source code for our example.

Sample Code


MetaPro Systems Inc. Visual Studio Dot Net Tips & Tricks #3 – Direct Access to
Your Outlook Address Book


Project Type: VS.NET Windows Application
Code Behind: Visual Basic

I have a project where I needed to access my Outlook Address book directly.
This is possible but very tricky. I got it to work and I would like to share
it with you. Thanks go to Jim Lennox of Scientific Solutions Inc. for helping
me work this out. I would also like to site the book "Developing Applications
using Outlook 2000, CDO, Exchange and Visual Basic" by Piemonte and Jamison as
a good source for this subject.

I have created a simple project to illustrate this principle. The project
reads the outlook address book and writes name, company and email to a text
file.

Here are the key portions of the code.

Dim moNS As Outlook.NameSpace
Dim mcContacts As Outlook.MAPIFolder
Dim moItems As Outlook.Items
Dim moCt As Outlook.ContactItem

moNS = mobjOutlook.GetNamespace("mapi")
mcContacts = _
moNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
' set to the contact folder
moItems = mcContacts.Items

moItems = moItems.Restrict("[MessageClass] = 'IPM.Contact'")
' filter to select only contact items

moItems.Sort("[EMail1Address]", False) ' sort by email address


Click below to get the complete source code for our example.

Sample Code


MetaPro Systems Inc. Visual Studio Dot Net Tips & Tricks #4 – Generic Data Classes


Project Type: VS.NET Windows Application
Code Behind: Visual Basic

For this tip something a little different. We are presenting two data access classes that use data sets to access any data table or query. They have been tested with MS Access and SQL Server but should work with any other relational database. A complete project is included that utilizes these classes. The project is Windows based but the classes should work with Web based projects as well. The sample project works with the Northwind database that comes with MS SQL Server. You can easily change the project to work with any database you wish.

You must change two lines in the app.config file. Just change the parameters of the value clause to match your database.

To access an Access database instead the value clause would look like this "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=G:\database\northwind.mdb". Note that for a web based project, these lines would be in the web.config file. There are two classes used to access your database tables: clsDatabase and clsDataset . Here is how you would create a class for a particular dataset.

dim gcloDatabase as new clsDatabase
dim mcloDataset as clsDataset
_SQL = "select * from Customers" _TableName = "Customers"
' replace these lines with any query you want
' table can also be a query
mclodataset = gcloDatabase.GetDataSet(_SQL, _TableName)
' to create the dataset table mclodataset.GetDataTable()
' this creates the a data table internally to dataclass mclodataset.HideColumn(0)
' this will hide a particular column the first in this case
' when you bind it to a grid
dtgCustomers.DataSource = mclodataset.ReturnDataTable ' bind your data table to a grid

Next I will describe some but not all of the methods that are part of the data set class. The rest is left to self-examination.

mMaxRowCount = mcloDataset.GetRowCount
' return the row count so you know when you have reached the end of the dataset.

mdrCustomer = mcloDataset.GetDataRow(mRowNumber)
' return a particular row based on the row number to a data row
' remember that the row count starts with zero and end with one
' less than the actual row count

txtCity.Text = mdrCustomer("City")
' retrieve a particular field from a data row

mdrCustomer("City") = txtCity.Text
' vice versa to make changes to your dataset

mcloDataset.UpdateDataAdapter()
' use this call to write any changes you have made to the dataset
' back to the database mcloDataset.RefreshDataset()
' this call is needed to repopulate the database with the latest changes to the dataset

mdrCurrent = mcloDataset.GetNewRow ' use this call to create a new empty row

mcloDataset.PutNewRow(mdrCurrent) ' add this call to put the new row to the dataset


Click below to get the complete source code for our example.

Sample Code