Microsoft KB Archive/319398

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

Article ID: 319398

Article Last Modified on 9/25/2002

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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 

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



Steps to Reproduce the Behavior
 Create a new Visual Basic Windows Application project. Form1 is created by default and shown in Design view. 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:  Right-click the toolbox, and then click Customize Toolbox. In the COM Components list, click to select Microsoft Office Spreadsheet 10.0 and Microsoft Office Chart 10.0, and then click OK.  Drag the Spreadsheet component from the toolbox to Form1, and then drag 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 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, &quot;B1&quot;) oSer.SetData(OWC10.ChartDimensionsEnum.chDimCategories, 0, &quot;A2:A3&quot;) oSer.SetData(OWC10.ChartDimensionsEnum.chDimValues, 0, &quot;B2:B3&quot;) oSer = oChart.SeriesCollection.Add oSer.SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, 0, &quot;C1&quot;) oSer.SetData(OWC10.ChartDimensionsEnum.chDimCategories, 0, &quot;A2:A3&quot;) oSer.SetData(OWC10.ChartDimensionsEnum.chDimValues, 0, &quot;C2:C3&quot;) </li>  Add the following code to the top of Form1.vb: Imports OWC10 = Microsoft.Office.Interop.OWC </li> Press F5 to build and run the program. You receive the error message that is described in the &quot;Symptoms&quot; section.</li>  Replace the following code AxChartSpace1.DataSource = AxSpreadsheet1 with: AxChartSpace1.DataSource = AxSpreadsheet1.GetOcx </li> Build and run the project. The form loads without any errors and the Chart binds to cells in the Spreadsheet component as expected.</li></ol>

<div class="references_section">