How to: Populate Documents with Data from a Database

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • Word 2007

  • Word 2003

For more information, see Features Available by Application and Project Type.

You can access data in document-level Visual Studio Tools for Office projects in the same way that you access data in Windows Forms projects. You use the same tools and code to bring the data from a database into your solution, and you can use Windows Forms controls to display the data.

In addition, you can display data by using host controls. Host controls are native objects in Microsoft Office Word that have been enhanced with events and data binding capability. For more information, see Host Items and Host Controls Overview.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can also access data in application-level projects, but only programmatically at run time.

The following example shows how to add data-bound controls in document-level projects using a designer. For an example of how to add data-bound controls in application-level projects at run time, see Walkthrough: Simple Data Binding in an Application-Level Project.

link to video For a related video demonstration, see Binding Data to Word 2007 Content Controls Using Visual Studio Tools for the Office System (3.0).

Adding a Control to a Document at Design Time

To populate a document with data from a database

  1. Open a Word document-level project in Visual Studio, with the document open in the designer.

  2. Open the Data Sources window and create a data source from a database. For more information, see How to: Connect to Data in a Database.

  3. Drag the field you want from the Data Sources window to your document.

One of the following controls is created on the document, depending on the type of project you are developing:

  • For Word 2007 projects, a content control is added to the document. The type of content control depends on the data type of the field you selected. For more information, see Content Controls.

  • For Word 2003 projects, a Bookmark control is added to the document. For more information, see Bookmark Control.

You can add a different control by selecting the data field in the Data Sources window and then choosing a different control from the drop-down list.

Objects in the Project

In addition to the control, the following data-related objects are automatically added to your project:

When you run the project, the control displays the first record in the data source. You can use the BindingSource to enable users to scroll through the records.

To scroll through the records

For information about how to send updates to the typed dataset and the database, see How to: Update a Data Source with Data from a Host Control.

See Also

Tasks

How to: Populate Documents with Data from Objects

How to: Update a Data Source with Data from a Host Control

Concepts

Binding Data to Controls in Office Solutions

Data Sources Overview

Displaying Data Overview

Using Local Database Files in Office Solutions Overview

Connecting to Data in Visual Studio Overview

Reference

BindingSource Component Overview

Other Resources

Binding Data to Word Content Controls Using Visual Studio Tools for the Office System

Change History

Date

History

Reason

July 2008

Added information about binding data in application-level add-ins.

SP1 feature change.