After you have created the Crystal Report file, the next
step is defining a data schema. After you have “informed” the Crystal Report
file of the data schema, a list of fields obtained from the data schema, will
appear in the field explorer. Once the fields appear in the field explorer, you
can "drag" the field to the report.
Before we discuss how to "inform" the Crystal
Report of the data schema, let us first discuss how to obtain the data schema.
Creating an XML data schema is as simple as calling the WriteXMLSchema method
off of a DataSet Object within code (VB or C#). The code below is a working
example of calling a method to retrieve a list of employees, converting the
list of employees into a DataSet object, and then writing the XML schema to a
file named EmployeeData.xml.
Listing 1
List<Employee> employees = GetEmployeesAsList();
DataSet employeeDataSet = ListToDataset<Employee>(employees);
employeeDataSet.WriteXmlSchema(@"C:\EmployeeData.xml");
Below is what the EmployeeData.xml contains. You will
notice each field is listed with the data type.
Listing 2
<?xml version="1.0" standalone="yes"?>
<xs:schema id="ArrayOfEmployee" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ArrayOfEmployee" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" minOccurs="0" />
<xs:element name="LastName" type="xs:string" minOccurs="0" />
<xs:element name="Address" type="xs:string" minOccurs="0" />
<xs:element name="City" type="xs:string" minOccurs="0" />
<xs:element name="State" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:string" minOccurs="0" />
<xs:element name="Role" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
To "inform" the Crystal Reports file of the data
schema, click on the Database Expert option in the toolbar, click Create New
Connection and select ADO.NET (XML) as shown in Figure 2, and select the data
schema that was just generated.
Figure 2: Setting the data connection to the
report
Once your data schema has been defined, you should see a
list of columns in the Field Explorer under Database Fields as shown in Figure
3.
Figure 3: Crystal Reports field expert
You can now “drag” your fields onto to your report.