• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

asp.net c# sample code errors

I am following a video to create a sample chart; link: http://www.asp.net/web-pages/videos/aspnet-razor-pages/displaying-data-in-a-chart-(part-1)

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 asp.net 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?)	
        chartImage.AddSeries(
                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;
0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 2
  • 2
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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.Series.Add("Decade");
            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

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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.

Regards,

Mike
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
0
 
Robert SchuttSoftware EngineerCommented:
Sure, if it's still unanswered, I'll have a go in a little bit.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now