Microsoft KB Archive/829115

= PRB: &quot;InvalidCastException&quot; Error Message When You Bind an Office 2003 Web Component on a .NET Windows Form =

Article ID: 829115

Article Last Modified on 10/23/2006

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * 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 

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



Steps to Reproduce the Problem
 Start Microsoft Visual Studio .NET 2003. Create a new Microsoft Visual Basic for Applications project.

By default, Form1 is created and appears in Design view. On the View menu, click Toolbox. If you do not have the Spreadsheet and the Chart in the toolbox, add them to the toolbox. To do this, follow these steps:  Right-click Toolbox. Click Add/Remove Items. If you are in Visual Studio. NET 2002, click Customize Toolbox.</li> In the COM Components list, click Microsoft Office Spreadsheet 11.0, click Microsoft Office Chart 11.0, and then click OK.</li></ol> </li> Move the Spreadsheet Component from the toolbox to Form1, and then move the Chart Component from the toolbox to Form1.</li>  Double-click Form1, and then add the following code to the Load event for Form1: 'Add data to the spreadsheet. Dim a1 As Object = {&quot;&quot;, 2001, 2002} Dim a2 As Object = {&quot;Region1&quot;, 1803, 2011} Dim a3 As Object = {&quot;Region2&quot;, 2103, 2500} With AxSpreadsheet1.ActiveSheet .Range(&quot;A1:C1&quot;).Value = a1   .Range(&quot;A2:C2&quot;).Value = a2    .Range(&quot;A3:C3&quot;).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, &quot;B1&quot;) oSer.SetData(OWC11.ChartDimensionsEnum.chDimCategories, 0, &quot;A2:A3&quot;) oSer.SetData(OWC11.ChartDimensionsEnum.chDimValues, 0, &quot;B2:B3&quot;) oSer = oChart.SeriesCollection.Add oSer.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames, 0, &quot;C1&quot;) oSer.SetData(OWC11.ChartDimensionsEnum.chDimCategories, 0, &quot;A2:A3&quot;) oSer.SetData(OWC11.ChartDimensionsEnum.chDimValues, 0, &quot;C2:C3&quot;) </li> Press F5 to build and to run the program.

You receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li>  Locate the following code: AxChartSpace1.DataSource = AxSpreadsheet1 Replace the previous code with the following code: AxChartSpace1.DataSource = AxSpreadsheet1.GetOcx </li> 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.</li></ol>

Additional query words: office2003 web component exception binding interop datasource

<div class="references_section">