Symptom
How to add a database field to a subreport at runtime using Crystal
Reports XI Release 2 In-Proc RAS.
Resolution
You can use the following .NET code to add a database object
to a subreport using CR XI Release 2 SP 2 In-Proc RAS. In-Proc
RAS is available after installing CR XI Release 2 Service Pack
2.
.NET Code:
Imports
CrystalDecisions.ReportAppServer.ClientDoc
Imports CrystalDecisions.ReportAppServer.Controllers
Imports CrystalDecisions.ReportAppServer.DataDefModel
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportAppServer.Controllers
Imports CrystalDecisions.ReportAppServer.DataDefModel
Imports CrystalDecisions.CrystalReports.Engine
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim boSubreportClientDocument As
CrystalDecisions.ReportAppServer.Controllers.SubreportClientDocument
Dim boParagraphs As
CrystalDecisions.ReportAppServer.ReportDefModel.Paragraphs
Dim boParagraph As CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph
Dim boParagraphElements As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphElements
Dim boParagraphTextElement As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement
Dim boFontColor As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
Dim boFont As CrystalDecisions.ReportAppServer.ReportDefModel.Font
Dim boParagraph As CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph
Dim boParagraphElements As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphElements
Dim boParagraphTextElement As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement
Dim boFontColor As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
Dim boFont As CrystalDecisions.ReportAppServer.ReportDefModel.Font
Dim boSection As
CrystalDecisions.ReportAppServer.ReportDefModel.Section
Dim boFieldObject1 As
CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject
Dim boFieldObject2 As CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject
Dim boFieldObject2 As CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject
Dim boTextObject1 As
CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim boTextObject2 As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim boTextObject2 As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim boSection1 As
CrystalDecisions.ReportAppServer.ReportDefModel.Section
Dim boSection2 As CrystalDecisions.ReportAppServer.ReportDefModel.Section
Dim boSection2 As CrystalDecisions.ReportAppServer.ReportDefModel.Section
Dim boArea1 As
CrystalDecisions.ReportAppServer.ReportDefModel.Area
Dim boArea2 As CrystalDecisions.ReportAppServer.ReportDefModel.Area
Dim boArea2 As CrystalDecisions.ReportAppServer.ReportDefModel.Area
Dim m_crNetDoc As New CrystalReport1
Dim m_RasDoc As ISCDReportClientDocument
m_RasDoc = m_crNetDoc.ReportClientDocument
Dim m_RasDoc As ISCDReportClientDocument
m_RasDoc = m_crNetDoc.ReportClientDocument
boSection1 = New
CrystalDecisions.ReportAppServer.ReportDefModel.Section
boSection2 = New CrystalDecisions.ReportAppServer.ReportDefModel.Section
boTextObject1 = New CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
boTextObject2 = New CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
boSection2 = New CrystalDecisions.ReportAppServer.ReportDefModel.Section
boTextObject1 = New CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
boTextObject2 = 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 the text value for the text field
boParagraphTextElement.Text = "This is newly added section in our report !!"
boParagraphTextElement.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrParagraphElementKindEnum.crParagraphElementKindText
boParagraphTextElement.Text = "This is newly added section in our report !!"
boParagraphTextElement.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrParagraphElementKindEnum.crParagraphElementKindText
'fix some wierd defaults and behavior by explicitly setting default
font
boFontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
boFont = New CrystalDecisions.ReportAppServer.ReportDefModel.Font
boFont.Bold = False
boFont.Name = "Arial"
boFontColor.Font = boFont
boParagraphTextElement.FontColor = boFontColor
boFontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
boFont = New CrystalDecisions.ReportAppServer.ReportDefModel.Font
boFont.Bold = False
boFont.Name = "Arial"
boFontColor.Font = boFont
boParagraphTextElement.FontColor = boFontColor
boParagraphElements.Add(boParagraphTextElement)
boParagraph.ParagraphElements = boParagraphElements
boParagraphs.Add(boParagraph)
boParagraph.ParagraphElements = boParagraphElements
boParagraphs.Add(boParagraph)
boTextObject1.Paragraphs = boParagraphs
boTextObject2.Paragraphs = boParagraphs
boTextObject2.Paragraphs = boParagraphs
'Now add it to the section
boTextObject1.Left = 1
boTextObject1.Top = 1
boTextObject1.Width = 4000
boTextObject1.Height = 226
boTextObject1.Left = 1
boTextObject1.Top = 1
boTextObject1.Width = 4000
boTextObject1.Height = 226
boTextObject2.Left = 1
boTextObject2.Top = 1
boTextObject2.Width = 4000
boTextObject2.Height = 226
boTextObject2.Top = 1
boTextObject2.Width = 4000
boTextObject2.Height = 226
boArea1 =
m_RasDoc.ReportDefController.ReportDefinition.ReportHeaderArea
boArea2 = m_RasDoc.ReportDefController.ReportDefinition.DetailArea
boArea2 = m_RasDoc.ReportDefController.ReportDefinition.DetailArea
'Set the properties for the section
boSection1.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrAreaSectionKindEnum.crAreaSectionKindReportHeader
boSection1.Name = "myheader"
boSection1.Height = 1000
boSection1.Width = 11520
boSection1.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrAreaSectionKindEnum.crAreaSectionKindReportHeader
boSection1.Name = "myheader"
boSection1.Height = 1000
boSection1.Width = 11520
'Set the properties for the section
boSection2.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrAreaSectionKindEnum.crAreaSectionKindDetail
boSection2.Name = "mydetailsection"
boSection2.Height = 1000
boSection2.Width = 11520
boSection2.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrAreaSectionKindEnum.crAreaSectionKindDetail
boSection2.Name = "mydetailsection"
boSection2.Height = 1000
boSection2.Width = 11520
'Create the field object that we will add to the report and set all of its properties
boFieldObject1 = New CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject
'Set which field to use for the data to be displayed
boFieldObject1.DataSourceName = "{Orders.Order ID}"
boFieldObject1.FieldValueType = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeInt32sField
boFieldObject1.Left = 1 * 1440 '1440 twips per inch
boFieldObject1.Width = 3 * 1440
boFieldObject1.FontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
boFieldObject1.FontColor.Font.Name = "Arial"
boFieldObject1.FontColor.Font.Size = 10
boFieldObject1.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft
boFieldObject1.Width = 3 * 1440
boFieldObject1.FontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
boFieldObject1.FontColor.Font.Name = "Arial"
boFieldObject1.FontColor.Font.Size = 10
boFieldObject1.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft
'Create the field object that we will add to the report and set all of its properties
boFieldObject2 = New CrystalDecisions.ReportAppServer.ReportDefModel.FieldObject
'Set which field to use for the data to be displayed
boFieldObject2.DataSourceName = "{Orders.Ship Via}"
boFieldObject2.FieldValueType = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeStringField
boFieldObject2.Left = 2 * 1440 '1440 twips per inch
boFieldObject2.Width = 3 * 1440
boFieldObject2.FontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
boFieldObject2.FontColor.Font.Name = "Arial"
boFieldObject2.FontColor.Font.Size = 10
boFieldObject2.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft
boFieldObject2.Width = 3 * 1440
boFieldObject2.FontColor = New CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
boFieldObject2.FontColor.Font.Name = "Arial"
boFieldObject2.FontColor.Font.Size = 10
boFieldObject2.Format.HorizontalAlignment = CrystalDecisions.ReportAppServer.ReportDefModel.CrAlignmentEnum.crAlignmentLeft
' Add a section to Report header
m_RasDoc.ReportDefController.ReportSectionController.Add(boSection1, boArea1, -1)
' Add a section to detail section
m_RasDoc.ReportDefController.ReportSectionController.Add(boSection2, boArea2, -1)
m_RasDoc.ReportDefController.ReportSectionController.Add(boSection2, boArea2, -1)
m_RasDoc.ReportDefController.ReportObjectController.Add(boTextObject1,
boSection1, -1)
m_RasDoc.ReportDefController.ReportObjectController.Add(boTextObject2, boSection2, -1)
m_RasDoc.ReportDefController.ReportObjectController.Add(boTextObject2, boSection2, -1)
boSubreportClientDocument =
m_RasDoc.SubreportController.ImportSubreport("report1",
Application.StartupPath & "\report1.rpt",
boSection1)
'Get the first section in the details section
boSection = boSubreportClientDocument.ReportDefController.ReportDefinition.DetailArea.Sections(0)
boSection = boSubreportClientDocument.ReportDefController.ReportDefinition.DetailArea.Sections(0)
boSubreportClientDocument.ReportDefController.ReportObjectController.Add(boFieldObject1,
boSection, -1)
boSubreportClientDocument.ReportDefController.ReportObjectController.Add(boFieldObject2, boSection, -1)
boSubreportClientDocument.ReportDefController.ReportObjectController.Add(boFieldObject2, boSection, -1)
CrystalReportViewer1.ReportSource =
m_RasDoc.ReportSource
End Sub
Keywords
How to add a database field in a subreport using In-Proc RAS, adding database object,adding database field, adding fields at runtime. , 7532875 , KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , How To
Product
SAP Crystal Reports XI ; SAP Crystal Reports XI R2