Symptom
- Attempting to retrieve objects from a report
For i = 0 To crReport.ReportDefinition.ReportObjects.Count
ReportType = crReport.ReportDefinition.ReportObjects(i).GetType.ToString
- This results in the following error:
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
Environment
- Crystal Reports for Visual Studio 2010
- Visual Studion 2010
Reproducing the Issue
- Use the following code block:
Dim ReportType As String = vbNull
Dim ReportTypeCriteria As String = "CrystalDecisions.CrystalReports.Engine.FieldObject"
Dim crReport As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim i As Int32
Dim fieldobj As CrystalDecisions.CrystalReports.Engine.FieldObject = Nothing
CrystalReport.Equals(Server.MapPath("JobReport.rpt"))
CrystalReport.FileName = Server.MapPath("JobReport.rpt")
CrystalReportViewer1.ReportSource = Session("CrystalReportViewer.ReportSource")
CrystalReportViewer1.DataBind()
For i = 0 To crReport.ReportDefinition.ReportObjects.Count
ReportType = crReport.ReportDefinition.ReportObjects(i).GetType.ToString
If ReportType.Equals(ReportTypeCriteria) Then
fieldobj = DirectCast(crReport.ReportDefinition.ReportObjects(i), CrystalDecisions.CrystalReports.Engine.FieldObject)
With fieldobj.ObjectFormat
.EnableCanGrow = True
.HorizontalAlignment() = CrystalDecisions.Shared.Alignment.LeftAlign
End With
End If
Next
2. Run a report
3. The error will be thrown at the following line of code:
ReportType = crReport.ReportDefinition.ReportObjects(i).GetType.ToString
Cause
The index is set incorrectly:
For i = 0 To crReport.ReportDefinition.ReportObjects.Count
ReportType = crReport.ReportDefinition.ReportObjects(i).GetType.ToString
Resolution
- Rather than using:
For i = 0 To crReport.ReportDefinition.ReportObjects.Count
- Use:
For i = 0 To crReport.ReportDefinition.ReportObjects.Count - 1
Keywords
CR2010 2010 CR4VS2010 CR4VS VS2010 NET2010 Crystal Reports for .NET Framework 4.0 crforvs 13 indexing count bad code SDK solution developer kit VS , KBA , BI-DEV-NET-SDK , .NET SDK / COM SDK , How To
Product
SAP Crystal Reports, developer version for Microsoft Visual Studio