PRB: "InvalidCastException" Error Message When You Bind an Office 2003 Web Component on a .NET Windows Form (829115)



The information in this article applies to:

  • Microsoft Office 2003 Web Components
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1

For a Microsoft Office XP Web Components version of this article, see 319398.

SYMPTOMS

When you bind a Microsoft Office 2003 Web Component to another Office 2003 Web Component for a DataSource property 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 receive this error message when you bind a Microsoft Office Spreadsheet Component, a Microsoft Office PivotTable Component, or a Microsoft Office 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. The GetOcx method is intended to act as the DataSource. The following is an example:
AxChartspace1.DataSource = AxSpreadsheet1.GetOcx()

MORE INFORMATION

Steps to Reproduce the Problem

  1. Start Microsoft Visual Studio .NET 2003.
  2. Create a new Microsoft Visual Basic for Applications project.

    By default, Form1 is created and appears in Design view.
  3. On the View menu, click Toolbox.
  4. If you do not have the Spreadsheet and the Chart in the toolbox, add them to the toolbox. To do this, follow these steps:
    1. Right-click Toolbox. Click Add/Remove Items. If you are in Visual Studio. NET 2002, click Customize Toolbox.
    2. In the COM Components list, click Microsoft Office Spreadsheet 11.0, click Microsoft Office Chart 11.0, and then click OK.
  5. Move the Spreadsheet Component from the toolbox to Form1, and then move the Chart Component from the toolbox to Form1.
  6. 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 then specify the chart dimensions.
    Dim oChart As OWC11.ChChart, oSer As OWC11.ChSeries
    oChart = AxChartSpace1.Charts.Add()
    oChart.HasLegend = True
    oSer = oChart.SeriesCollection.Add
    oSer.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, 0, "B1")
    oSer.SetData(OWC11.ChartDimensionsEnum.chDimCategories, 0, "A2:A3")
    oSer.SetData(OWC11.ChartDimensionsEnum.chDimValues, 0, "B2:B3")
    oSer = oChart.SeriesCollection.Add
    oSer.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, 0, "C1")
    oSer.SetData(OWC11.ChartDimensionsEnum.chDimCategories, 0, "A2:A3")
    oSer.SetData(OWC11.ChartDimensionsEnum.chDimValues, 0, "C2:C3")
  7. Press F5 to build and to run the program.

    You receive the error message that is mentioned in the "Symptoms" section.
  8. Locate the following code:
    AxChartSpace1.DataSource = AxSpreadsheet1
    Replace the previous code with the following code:
    AxChartSpace1.DataSource = AxSpreadsheet1.GetOcx()
  9. Build and then run the project.

    The form loads without any errors, and the Chart binds to the cells in the Spreadsheet Component as you expect.

REFERENCES

For additional information about Microsoft Office Web Components object model, see the Microsoft Office Web Components Visual Basic Reference (Owcvba11.chm). The default installation path for this reference is:

C:\Program Files\Common Files\Microsoft Shared\Web Components\11\1033


Modification Type:MajorLast Reviewed:10/28/2003
Keywords:kbOfficeWebChart kbOfficeWebPivot kbOfficeWebSpread kbprb KB829115 kbAudDeveloper