Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.07 Selecting a Field

Selecting a Field from a Data Source

The report’s data source points to all the tables and fields that can be printed. Even if a field isn’t displayed on the report, it will still be in the data source if the report’s query specifies it to be available. The RAS object model has methods within the ReportClientDocument.Database class that make it easy to find the table and field you need. As you can see in the comments within the code listing, you first get a reference to the table using the FindAlias() method and pass it the table name. It returns the index of the table in the Tables collection. The next step calls the Table.DataFields.Find() method and passes it the field name. This returns the index of the field in the DataFields collection. A reference to this field is returned to the calling method.

Listing 20-5. Get a reference to a field in the data source.
[VB.NET]
Public Shared Function GetField(ByVal TableName As String, ByVal FieldName As String, ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument) As CrystalDecisions.ReportAppServer.DataDefModel.Field
Dim FieldIndex As Integer
Dim TableIndex As Integer
'Find the table's location in the table collection
Dim myTable As CrystalDecisions.ReportAppServer.DataDefModel.Table
Dim myField As CrystalDecisions.ReportAppServer.DataDefModel.Field
TableIndex = rcd.Database.Tables.FindByAlias(TableName)
Dim temp As CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable
temp = rcd.Database.Tables(TableIndex)
'Get a reference to the table object
myTable = DirectCast(temp, CrystalDecisions.ReportAppServer.DataDefModel.Table)
'Find the field's location in the field collection
FieldIndex = myTable.DataFields.Find(FieldName, CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName, CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault)
'Get a reference to the field object
myField = DirectCast(myTable.DataFields(FieldIndex), CrystalDecisions.ReportAppServer.DataDefModel.Field)
Return myField
End Function
[C#]
public static CrystalDecisions.ReportAppServer.DataDefModel.Field GetField(string TableName, string FieldName,
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
int FieldIndex, TableIndex;
CrystalDecisions.ReportAppServer.DataDefModel.Table myTable;
CrystalDecisions.ReportAppServer.DataDefModel.Field myField;
//Find the table's location in the table collection
TableIndex = rcd.Database.Tables.FindByAlias(TableName);
CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable temp;
temp = rcd.Database.Tables[TableIndex];
//Get a reference to the table object
myTable = (CrystalDecisions.ReportAppServer.DataDefModel.Table)temp;
//Find the field's location in the field collection
FieldIndex = myTable.DataFields.Find(FieldName,
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName,
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);
//Get a reference to the field object
myField = (CrystalDecisions.ReportAppServer.DataDefModel.Field)myTable.DataFields[FieldIndex];
return myField;
}