PRB: InvalidCastException When You Bind Office XP Web Component on a .NET Windows Form (319398)



The information in this article applies to:

  • Microsoft Office XP Web Components
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q319398

SYMPTOMS

When you bind an Office XP Web Component to another Office XP Web Component for a DataSource at run time, you receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in <YourWindowsApplication.exe>
Additional information: Specified cast is not valid.
You might receive this error when you bind a Spreadsheet Component, a PivotTable Component, or a Chart Component to one of the other Office Web Components. For example, the following line of code generates the error:
AxChartspace1.DataSource = AxSpreadsheet1
				

RESOLUTION

To resolve this problem, set the DataSource property for the component to the object that the GetOcx method of the control returns, which is intended to act as the DataSource. The following is an example:
AxChartspace1.DataSource = AxSpreadsheet1.GetOcx()
				

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Basic Windows Application project. Form1 is created by default and shown in Design view.
  2. On the View menu, click Toolbox. If you do not have the Spreadsheet and Chart in the toolbox, add them to the toolbox. To do this, follow these steps:
    1. Right-click the toolbox, and then click Customize Toolbox.
    2. In the COM Components list, click to select Microsoft Office Spreadsheet 10.0 and Microsoft Office Chart 10.0, and then click OK.
  3. Drag the Spreadsheet component from the toolbox to Form1, and then drag the Chart component from the toolbox to Form1.
  4. Double-click Form1, and then add the following code to the Load event for Form1:
    'Add data to the spreadsheet.
    Dim a1 As Object() = {"", 2001, 2002}
    Dim a2 As Object() = {"Region1", 1803, 2011}
    Dim a3 As Object() = {"Region2", 2103, 2500}
    With AxSpreadsheet1.ActiveSheet
        .Range("A1:C1").Value = a1
        .Range("A2:C2").Value = a2
        .Range("A3:C3").Value = a3
    End With
    
    'Bind the chart to the spreadsheet.
    AxChartSpace1.DataSource = AxSpreadsheet1 
    
    'Add a new chart and specify the chart dimensions.
    Dim oChart As OWC10.ChChart, oSer As OWC10.ChSeries
    oChart = AxChartSpace1.Charts.Add()
    oChart.HasLegend = True
    oSer = oChart.SeriesCollection.Add
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, 0, "B1")
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimCategories, 0, "A2:A3")
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimValues, 0, "B2:B3")
    oSer = oChart.SeriesCollection.Add
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, 0, "C1")
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimCategories, 0, "A2:A3")
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimValues, 0, "C2:C3")
    					
  5. Add the following code to the top of Form1.vb:
    Imports OWC10 = Microsoft.Office.Interop.OWC
    					
  6. Press F5 to build and run the program. You receive the error message that is described in the "Symptoms" section.
  7. Replace the following code
    AxChartSpace1.DataSource = AxSpreadsheet1 
    					
    with:
    AxChartSpace1.DataSource = AxSpreadsheet1.GetOcx()
    					
  8. Build and run the project. The form loads without any errors and the Chart binds to cells in the Spreadsheet component as expected.

REFERENCES

For additional information about the Office XP Web Components object model, see the Microsoft Office Web Components Visual Basic Reference (Owcvba10.chm). The default installation path for this reference is C:\Program Files\Common Files\Microsoft Shared\Web Components\10\1033\.

Modification Type:MajorLast Reviewed:9/25/2002
Keywords:kbOfficeWebChart kbOfficeWebPivot kbOfficeWebSpread kbprb KB319398