Solved c# sample code errors

Posted on 2014-08-03
Last Modified: 2014-08-04
I am following a video to create a sample chart; link:

I am getting a 2 errors (I solved error 1) most likely because the author is using MVC, layout etc but I am creating it in vs2010 4.0 c#.

Question: Could you please take a look at this code and show me how I can handle these errors?

Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

public partial class z_CrimeRate : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        var chartImage = new Chart(600, 400);  //1: Error 57 'System.Web.UI.DataVisualization.Charting.Chart' does not contain a constructor that takes 2 arguments	

        chartImage.AddTitle("Sample Chart");   //2: Error 59 'System.Web.UI.DataVisualization.Charting.Chart' does not contain a definition for 'AddSeries' and no extension method 'AddSeries' accepting a first 
                                               //argument of type 'System.Web.UI.DataVisualization.Charting.Chart' could be found (are you missing a using directive or an assembly reference?)	
                Name: "Decade",
                axisLabel: "Incidence",
                xValue: new[] {"1970","1980","1990","2000","2010"},
                yValues: new[] {"9","22","52","89","123"});
    chartImage.write();            //3: Error 54 Invalid token '(' in class, struct, or interface member declaration	


Open in new window

Error 1 was resolved doing:
var chartImage = new Chart();        
        chartImage.Width = 600;
        chartImage.Height = 400;
Question by:Mike Eghtebas
    LVL 35

    Accepted Solution

    Not sure what that sample code is doing, seems to be for a different class altogether (didn't watch the video)...

    Here's a simple example for a number of 'standard' ASP.NET chart properties, if you want it dynamically generated:
            protected void Page_Load(object sender, EventArgs e) {
                var chartImage = new Chart();
                chartImage.Width = 600;
                chartImage.Height = 400;
                chartImage.Titles.Add("Sample Chart");
                chartImage.Legends.Add(chartImage.Series[0].Name); // optional, show legend
                // add data points
                object[] xValue = { "1970", "1980", "1990", "2000", "2010" };
                object[] yValues = { "9", "22", "52", "89", "123" };
                for (int x = 0; x < xValue.Length; x++) {
                    chartImage.Series[0].Points.AddXY(xValue[x], yValues[x]); // may need to cast to double, if "option strict"
                chartImage.ChartAreas.Add("chartArea0"); // always need to add this
                chartImage.ChartAreas[0].AxisX.Title = "Decade"; // optional, show axis x title
                chartImage.ChartAreas[0].AxisY.Title = "Incidence"; // optional, show axis y title
                chartImage.ImageStorageMode = ImageStorageMode.UseImageLocation; // avoid having to define http handler for now, simpler for quick testing
                this.Controls.Add(chartImage); // add to page for automatic rendering

    Open in new window

    LVL 33

    Author Closing Comment

    by:Mike Eghtebas
    Thank you very much. I have some followup questions on this chart. I will post the links here for the new question if you have some time to post a comment for it.


    LVL 33

    Author Comment

    by:Mike Eghtebas
    LVL 35

    Expert Comment

    by:Robert Schutt
    Sure, if it's still unanswered, I'll have a go in a little bit.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now