Using a Database as a Data Source


WordWriter can populate template merge fields with values from a database record. To use a database record as a data source, pass the SetDataSource method either an ADO.NET DataSet or an ADO.NET DataTable. If the specified DataSet or DataTable contains more than one row, WordWriter will use the first row as the data source.

The DataSet and DataTable objects are in the System.Data namespace. Use an Import directive to import the namespace to the aspx page:

	<%@ Import namespace="System.Data" %>

The WordWriter database samples get data from an Access database (Northwind.mdb), accessible through OleDb. The OleDb objects are in the System.Data.OleDb namespace:

	<%@ Import namespace="System.Data.OleDb" %>

Using an ADO.NET DataSet as a Data Source
VB.NET Sample
Basic/DataSet/DataSet-vb.aspx

[View Source]

C# Sample
Basic/DataSet/DataSet-cs.aspx

[View Source]

A DataSet contains a collection of DataTables. If you set a template's data source to a DataSet, WordWriter will get the first row of the first DataTable in the DataSet. To pass a DataSet to SetDataSource:

  1. After creating and opening the database connection, create an OleDbDataAdapter that will execute a SQL command at the data source and fill the DataSet, for example:
    	Dim strSQL As String = "SELECT TOP 1 TitleOfCourtesy, FirstName, LastName,_
    		Title FROM Employees"
    	...
    	Dim oAdpt As OleDbDataAdapter = Nothing
    	...
    	oAdpt = New OleDbDataAdapter(strSQL, oConn)
  2. Create a DataSet:
    	oDS = New DataSet()
  3. Use OleDbDataAdapter.Fill to populate the DataSet with the result of the SQL query, for example:
    	oAdpt.Fill(oDS)
  4. When you call WordTemplate.SetDataSource, pass the DataSet to the method, for example:
    	oWW = New WordTemplate()
    	oWW.Open(Server.MapPath("./DataSetTemplate.doc"))
    	oWW.SetDataSource(oDS)

Top


Using an ADO.NET DataTable as a Data Source
VB.NET Sample
Basic/DataTable/DataTable-vb.aspx

[View Source]

C# Sample
Basic/DataTable/DataTable-cs.aspx

[View Source]

If you set a template's data source to a DataTable, WordWriter will get the first row of the DataTable. To pass a DataTable to SetDataSource:

  1. After creating and opening the database connection, create an OleDbDataAdapter that will execute a SQL command at the data source and fill the DataSet, for example:
    	Dim strSQL As String = "SELECT TOP 1 TitleOfCourtesy, FirstName, LastName,_
    		Title FROM Employees"
    	...
    	Dim oAdpt As OleDbDataAdapter = Nothing
    	...
    	oAdpt = New OleDbDataAdapter(strSQL, oConn)
  2. Create a DataSet:
    	oDS = New DataSet()
  3. Use OleDbDataAdapter.Fill to populate the DataSet with the result of the SQL query, and add a table to the DataSet:
    	oAdpt.Fill(oDS, "myDataTable")
  4. Create a DataTable object:
    	oDT = oDS.Tables("myDataTable")
  5. When you call WordTemplate.SetDataSource, pass the DataTable to the method, for example:
    	oWW = New WordTemplate()
    	oWW.Open(Server.MapPath("./DataSetTemplate.doc"))
    	oWW.SetDataSource(oDT)

Dynamic Database Sample

The DatabaseSelect samples create a dynamic form letter, using a DataSet as the template's data source. The samples get an employee name from an HTML form, and use the name to get additional employee information from the Northwind Traders database (in WordWriter\doc-samples\database). To run either sample, click Run Sample, and click any employee's Go button.

VB.NET Sample
Intermediate/DatabaseSelect/DatabaseSelect-vb.aspx

[View Source]

C# Sample
Intermediate/DatabaseSelect/DatabaseSelect-cs.aspx

[View Source]


Copyright © 2003, SoftArtisans, Inc.