Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Dynamicaly bind 10 tables to crystal report

Hi to all,

Iu2019m using crystal report 11.5,Visual studio2005-C#,Asp.net 2.0 and SQL Server 2005;

I want to add the tables, fieldobejcts and blobjects dynamically based on the Dataset object. In my dataset object I have 10 tables each tables having one record and having different number of columns , my problem is when I add the Fieldobject to the Reportdocument , finaly I see only the last record from the last table in the viewer. But I want to bind every records from all tables. Here is my sample code

/// <summary>

/// Add the Table dynamically to the crystal report

/// </summary>

/// <returns></returns>

private void AddTableToCrystalReport()

{

try

{

ExportExcel objExport = (ExportExcel)vwExport;

//Create a new ReportDocument

boReportDocument = new ReportDocument();

// load the RPT file

boReportDocument.Load(objExport.Server.MapPath("~//Reports//cr_DynamicBinding.rpt"), OpenReportMethod.OpenReportByTempCopy);

//Access the ReportClientDocument in the ReportDocument (EROM bridge)

boReportClientDocument = boReportDocument.ReportClientDocument;

// Convert the DataSet to an ISCRDataset object (something the ISCDReportClientDocument can understand)

CrystalDecisions.ReportAppServer.DataDefModel.ISCRDataSet rasDS;

rasDS = CrystalDecisions.ReportAppServer.DataSetConversion.DataSetConverter.Convert(dsBindIntoCR);

// Add the dataset as a data source to the report

boReportClientDocument.DatabaseController.AddDataSource((object)rasDS);

// Add a field to the report canvas

// Note: This is quick and dirty. No positioning, resizing, formatting, etc.

CrystalDecisions.ReportAppServer.Controllers.ISCRResultFieldController rfc;

CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable crTable;

CrystalDecisions.ReportAppServer.DataDefModel.ISCRField crField;

CrystalDecisions.ReportAppServer.ReportDefModel.ISCRFieldHeadingObject fh;

rfc = boReportClientDocument.DataDefController.ResultFieldController;

//crTable = boReportClientDocument.Database.Tables[0];

}

catch (Exception ex)

{

throw ex;

}

}

//Get the first section in the details section

boSection = boReportClientDocument.ReportDefController.ReportDefinition.DetailArea.Sections[0];

dtlSection = boReportClientDocument.ReportDefController.ReportDefinition.ReportHeaderArea.Sections[0];

//Set the text value for the text field

for (int Row = 0; Row < dsBindIntoCR.Tables.Count; Row++)

{

for (int intCounter = 0; intCounter < dsBindIntoCR.Tables[Row].Columns.Count; intCounter++)

{

//Create the field object that we will add to the report and set all of its properties

boFieldObject = new CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject();

//Create the paragraph elements for the text Object

boTextObject = new CrystalDecisions.ReportAppServer.ReportDefModel.TextObject();

boParagraphs = new CrystalDecisions.ReportAppServer.ReportDefModel.Paragraphs();

boParagraph = new CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph();

boParagraphElements = new CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphElements();

boParagraphTextElement = new CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement();

//Set which field to use for the data to be displayed

boFieldObject.DataSource = "{Record" + Row.ToString() + "." + dsBindIntoCR.Tables[Row].Columns[intCounter].ColumnName.ToString().Trim() + "}";

boFieldObject.FieldValueType = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeStringField;

boFieldObject.Left = LeftPosDtl; //1440 twips per inch

boFieldObject.Top = TopPos;

LeftPosDtl += boFieldObject.Width;

//TopPos += boFieldObject.Height;

boFieldObject.Height = intMinHeightForFo;

boFieldObject.FontColor = new CrystalDecisions.ReportAppServer.ReportDefModel.FontColor();

boFieldObject.FontColor.Font.Name = "Verdana";

boFieldObject.FontColor.Font.Size = 10;

boFieldObject.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft;

//Set the text value for the text field

boParagraphTextElement.Text = dsBindIntoCR.Tables[Row].Columns[intCounter].ColumnName.ToString().Trim();

boParagraphTextElement.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrParagraphElementKindEnum.crParagraphElementKindText;

boParagraphElements.Add(boParagraphTextElement);

boParagraph.ParagraphElements = boParagraphElements;

boParagraphs.Add(boParagraph);

boTextObject.Paragraphs = boParagraphs;

//Now add it to the section

boTextObject.Left = LeftPosHdr;

LeftPosHdr += boTextObject.Width;

boTextObject.Top = 1;

intMinHeightForFo = intMinHeightForFo + 500;

intMinLeftPositionForFo = intMinLeftPositionForFo + 500;

intMinLeftPositionForTo = intMinLeftPositionForTo + 250;

intMinHeightForTo = intMinHeightForTo + 250;

int ImgHeight = 0;

if ((dsBindIntoCR.Tables[Row].Columns.Count - intCounter) == 2)

{

blnIsBlobObject = true;

boBFObject = new CrystalDecisions.ReportAppServer.ReportDefModel.BlobFieldObject();

boBFObject.DataSourceName = "{Record" + DataTablePointer.ToString() + ".Path1}";

boBFObject.Width = 1500 * 10;

boBFObject.Height = 1000 * 10;

boBFObject.Left = 0;

boTextObject.Top = 0;

ImgHeight = boTextObject.Height;

//boReportClientDocument.ReportDefController.ReportObjectController.Add(boBFObject, boSection, 1);

}

if ((dsBindIntoCR.Tables[Row].Columns.Count - intCounter) == 1)

{

blnIsBlobObject = true;

boBFObject = new CrystalDecisions.ReportAppServer.ReportDefModel.BlobFieldObject();

boBFObject.DataSourceName = "{Record" + DataTablePointer.ToString() + ".Path2}";

boBFObject.Width = 1500 * 10; ;

boBFObject.Left = 350;

boBFObject.Top = 800;

boBFObject.Height = 1000 * 10;

boReportClientDocument.ReportDefController.ReportObjectController.Add(boBFObject, boSection, 1);

}

//Add the object to the report

if (!blnIsBlobObject)

{

boReportClientDocument.ReportDefController.ReportObjectController.Add(boTextObject, dtlSection, -1);

boReportClientDocument.ReportDefController.ReportObjectController.Add(boFieldObject, boSection, -1);

}

}

}

CrystalReportViewer1.ReportSource = boReportDocument;

CrystalReportViewer1.DataBind();

Experts please tell me how to add the all table records to the crystal report

This is my uegent work experts please help me.

Thanks in advance

Former Member
replied

Hi;

Sorry, I should have been clearer. If you preview the report in your application, and then export it to Crystal Reports format, the exported report will have Saved Data with it. You can open it up in the CR Designer, and see if the tables were linked correctly or not.

Regards,

Jonathan

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question