Monday, July 5, 2010

Updating Microsoft Charting Controls for .NET from VS.NET 2008 to VS.NET 2010

If you recently did an upgrade of a project from VS.NET 2008 to 2010 that contained the Microsoft Charting controls for .NET, then you probably encountered some errors upon 1st run if you are now targeting the .NET Framework 4.0. The solution is to upgrade the charting references to the new .dll version throughout the application.

One possible exception you may have encountered immediately is the following:
"'Legend' is ambiguous in the namespace 'System.Web.UI.DataVisualization.Charting'."

This all points to the need to update the references to the 4.0.0.0 version of the System.Web.DataVisualization.dll used by the MS Chart controls. The following steps need to be completed to get your project current and working with the 4.0 version of the Chart controls.

1. Remove the reference to the System.Web.DataVisualization .dll in the project. If just converted it will still be referencing the 3.5.0.0 version of the .dll.

2. Add a reference to the new 4.0.0.0 version of the .dll from the '.NET' tab when adding a reference to your project. You can view the 'Version' column to make sure you have the correct reference.

3. In the web.config, update the version number in the 'Controls' tag from 3.5.0.0 to 4.0.0.0. Nothing else needs to be modified; the PublicKeyToken, etc. are all the same. Just change the version number. The corrected configuration is displayed below:

<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
4. In the web.config, update the version number in the 'assembly' tag for the System.Web.DataVisualization reference from 3.5.0.0 to 4.0.0.0 if not already done. This may have been updated when adding the reference in Step #2, but if not go ahead and modify it now. The corrected configuration is displayed below:

<add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
5. In the web.config, update the 'httpHandlers' section for "ChartImg.axd" version once again from 3.5.0.0 to 4.0.0.0. The corrected configuration is displayed below:

<httpHandlers>
<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
6. In the markup of any page using the Chart controls, remove the page registration for the assembly; just delete it. The next step will add back in the proper registration.

7. From the 'ToolBox' under the 'Data' tab, drag a 'Chart' control onto your existing page. This simple method will create the proper registration at the top of the page. Make sure to modify the 'TagPrefix' to the proper value if you had changed it previously from the default 'asp' used. After the registration is added back to the page, you can delete the chart control dragged on the page; it was just used to create the proper registration (this happens whenever a new control is dragged onto a page). Repeat this step for any pages that have a chart control on them. The proper page registration is shown below:

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
8. Rebuild your solution and run it. Your MS Chart controls should now be displayed properly!

As an additional reference, the MS Chart samples for .NET have been updated for the .NET Framework 4.0. If you have any additional problems, download the samples project and view the code or markup. You can find the samples at the following link:

Samples Environment for .NET Framework 4 Chart Controls Released!

5 comments:

  1. Thank you, this was helpful just upgraded my project from vs 2008 to vs 2010 and was suddenly getting errors.

    ReplyDelete
  2. Helped me, thanks. Search your project (use F3 key) for "3.5", you will find any references you've missed!

    ReplyDelete
  3. Hi,

    Thank you for this steps to be followed. But i was stuck up with the same error after doing all these.
    BC30560: 'Chart' is ambiguous in the namespace 'System.Web.UI.DataVisualization.Charting'.
    I added the dll into the application's bin folder, then got my application worked.

    ReplyDelete
  4. Really appreciate this post. I also had to copy the DLL to my application's bin folder. Thanks.

    ReplyDelete
  5. what DLL file should copy into project bin folder? Please help.

    ReplyDelete