Define the Dataset Schema File
When printing from any data source, Crystal Reports needs to know the tables and fields that it is printing from. When using a standard database, it can query the database to get this information. But when reporting from a dataset it doesn’t have this luxury. Instead, it uses a schema file to determine this information. It’s your job to define the dataset’s schema file and point the report to it.
A dataset schema is a fully compliant XML file that defines the properties of the data source. It usually has a file extension of ‘.xsd’. Listing 17-6 shows an excerpt of the XML code that is in the dataset file.
Listing 17-6. The XML schema from a dataset file.
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomerID" type="xs:string" />
<xs:element name="CompanyName" type="xs:string" />
<xs:element name="ContactName" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
Using the IDE to Create a Dataset File
The Visual Studio IDE makes it easy to create a dataset file. Select the menu options Project > Add New Item. The Add New Item dialog box lists the Dataset as an option. Click on this option, give it an appropriate file name and click Add. A blank dataset is created and added as an item in the Solution Explorer window. As you can see in Figure 17-2, the dataset is represented in the IDE as an empty screen.
Figure 17-2. The default Dataset designer surface.
At this point, the IDE is ready to build a new dataset file from scratch. Open the Server Explorer and drill down to the appropriate data source to find the table(s) you need. Drag one or more tables from the Server Explorer window onto the dataset surface. Behind the scenes the IDE generates the appropriate code to represent the table(s) with XML. To see what the XML source looks like, save the file and view the .XSD file that is saved in the solutions folder on your hard drive.
When selecting a field in the dataset, you can set its properties in the Properties window. This lets you change things such as the data type, caption, default value, etc. If you have a table that is similar to the dataset you want to create, but not exact, add it to the dataset and make the necessary changes to its properties. This is easier than creating the dataset from scratch.
Once finished adding the appropriate tables to the dataset your screen will show the tables you added and their relationships.
Figure 17-3. The Dataset designer surface with two sample tables.
Clicking on the Save button in the toolbar causes the IDE to save the XML source to the dataset file. The file is ready to be linked to the report.
It is easy to forget to save the schema file before linking it to the report. But if you don’t save the file, the Report Expert won’t know what the fields are. It will show an empty table with no fields listed. There must be a physical .xsd file prior to running the Report Expert.