0
votes

I'm making a dynamic report using Crystal Reports and .NET C#. The content of the report is not fully known until run-time. The report template only contains Formula Fields and a connection to the database table. At runtime I collect the wanted data collection, create a new reportDocument object and set the collection to the reports DataSource. Then I collect all field objects from the report template and loop through them, setting the data properties. This works fine as long as the properties is of type string, BUT when it is of type bool or double, it crashes with the following error: "Invalid object format name"

How can I set the data type of the field object to numeric/bool at runtime?

Here's some code:

//Collecting data and setting data source
for (int i = 0; i < ColNumber; i++)
            {
                //Fields 

                var DbFieldName = mappingNames[i];
                var fieldType = comparisonObj.GetFieldType(DbFieldName);

                var fieldName = "Field" + (i + 1);
                var ffld = report.DataDefinition.FormulaFields[fieldName];
                if (ffld != null)
                {
                    ffld.Text = "{" + aliasName + "." + DbFieldName + "}";                        
                }


                var fobj = report.ReportDefinition.ReportObjects[fieldName] as FieldObject;
                if (fobj != null)
                {
                    fobj.Left = CurrentLeft;
                    fobj.Width = FieldSpace * widths[i] / 100;
                    if (IsNumber(fieldType))
                    {
                        fobj.ObjectFormat.HorizontalAlignment = Alignment.RightAlign;
                        fobj.FieldFormat.NumericFormat.RoundingFormat = RoundingFormat.RoundToTen;
                    }
                    else if (IsBool(fieldType))
                    {
                        fobj.FieldFormat.BooleanFormat.OutputType = BooleanOutputType.YesOrNo;
                    }

                }

                //Headers
                var ColumnHeaderName = headers[i];
                var columnName = "Column" + (i + 1);
                var tobj = report.ReportDefinition.ReportObjects[columnName] as TextObject;
                if (tobj != null)
                {
                    tobj.Text = ColumnHeaderName;
                    tobj.Left = CurrentLeft;
                    tobj.Width = FieldSpace * widths[i] / 100;
                    CurrentLeft = CurrentLeft + tobj.Width + blank;
                    if (IsNumber(fieldType))
                        tobj.ObjectFormat.HorizontalAlignment = Alignment.RightAlign;
                }

            }
1

1 Answers

0
votes

Well, since nobody could help me with a solution, I figured one out for my self. I converted booleans and numbers into strings using the toString method. Not the best solution (in my eyes), cause I wanted to change the type of the field object it self, but it works.

                var DbFieldName = mappingNames[i];
                var fieldType = comparisonObj.GetFieldType(DbFieldName);

                var fieldName = "Field" + (i + 1);
                var ffld = report.DataDefinition.FormulaFields[fieldName];
                if (ffld != null)
                {
                    if (IsNumber(fieldType))
                        ffld.Text = "ToText({" + aliasName + "." + DbFieldName + "}, 00, '')";
                    else if (IsDateTime(fieldType))
                        ffld.Text = "ToText({" + aliasName + "." + DbFieldName + "}, \"dd/MM/yyyy\")";
                    else if (IsBool(fieldType))
                        ffld.Text = "if{" + aliasName + "." + DbFieldName + "} then \"Y\" else \"N\"";
                    else
                    {
                        ffld.Text = "{" + aliasName + "." + DbFieldName + "}";
                    }
                }