Online Book Reader

Home Category

Access Cookbook - Ken Getz [320]

By Root 1870 0
of this chapter's sample files using the following syntax (see topic 17.1 for more details on running the RegAsm utility):

regasm Geometry.dll /tlb: Geometry.tlb /codebase

RegAsm will display a warning about this being an unsigned assembly but you can safely ignore the warning.

Restart Access and load the 17-02.MDB database. Open the frmCircleUsingHelper form in design view.

From the VBA IDE, select Tools → References. Verify that the Geometry component is selected.

Note the source code behind the Calculate command button:

Private Sub cmdCalculate_Click( )

Dim cirCOM As Geometry.CircleCOM

Set cirCOM = New Geometry.CircleCOM

cirCOM.Radius = txtRadius

lblMsg.Caption = "Area of circle: " & cirCOM.Area( )

End Sub

Close and save the form.

Open frmCircleUsingHelper in form view. Enter a numeric value into the Radius textbox and click the Calculate button. Access should display the result as shown in Figure 17-4.

Figure 17-4. frmCircleUsingHelper instantiates a helper class, CircleCOM, which calls the inaccessible class, Circle

Discussion


The helper class could have been constructed in a number of ways. Although we chose to use a derived class, the helper class could also have been independent of the original class. The helper class could live within the same component or in a separate component. In this example, we chose to make the helper class a derived class that lives in the same component as the inaccessible class.

In this example, you were able to use the CircleCOM class to call the Circle class. In instantiating the Circle class, CircleCOM passed a dummy radius value to the constructor. Because Circle also included a Radius property, you were able to specify the radius value prior to calling the Area method. There may be some classes where properties that duplicate the constructor parameters are not available. In these cases, it may be difficult if not impossible to create a helper class that is able to instantiate the inaccessible class for you.

Many of the built-in classes of the .NET Framework contain parameterized constructors. This means that you will need to create a lot of helper classes in order to work with these classes.

17.3. Retrieve Access Data from an ASP.NET Application


Problem


Your ASP.NET web site needs to access data from one of your Access databases. How do you retrieve Access data using ADO.NET?

Solution


Follow these steps to create an ASP.NET page, AltRock.aspx, which displays a list of alternative rock albums from the 17-03.MDB database using a DataGrid control:

Start Visual Studio .NET.

Create a new Visual Basic .NET ASP.NET Web Application project.

Under location, enter "http://localhost/Music" and click OK.

Delete the initial WebForm1.aspx file from the project.

Select Project → Add Web Form... to add a new web form page to the project named AltRock.aspx.

With the Web Forms toolbox tab visible, drag a DataGrid control to the page.

Using the Property sheet, change the ID of the new DataGrid control to dgrAltRock.

Right-click on the DataGrid control and select Auto Format... from the popup menu. Select a format of your liking and click OK.

Select View → Code to jump to the code editor.

Add the following code to the very top of the page (above the Class statement) to import the System.Data.OleDb namespace:

Imports System.Data.OleDb

Add the following code to the Page_Load event handler to establish a connection to the 17-03.MDB database:

' You will need to edit the Data Source value to correspond

' to the location of the 17-03.mdb database on your system.

Dim cnx As OleDbConnection = _

New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=D:\Books\AccessCookBook2003\ch17\17-03.mdb")

cnx.Open( )

As noted by the comment in the code you will need to edit the path to the 17-03.MDB database to match where the database is located on your system.

Add the following code to retrieve the rows returned by the database's qryAlternativeAlbums query as a OleDbDataReader:

' Constuct

Return Main Page Previous Page Next Page

®Online Book Reader