WebChart Demo

This demo illustrates how to display data from a DataSet in a WebChart control.

System.ApplicationException: Error while rendering the chart. ---> System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at WebChart.ChartEngine.SaveToFile(String filename, ImageFormat format)
   at WebChart.ChartControl.RedrawChart()
   --- End of inner exception stack trace ---
   at WebChart.ChartControl.RedrawChart()
   at WebChart.ChartControl.RedrawChart()

Source Code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ register tagprefix="web" namespace="WebChart" assembly="WebChart"%>
<script runat="server" language="VB">

  Sub Page_Load(sender as Object, e as EventArgs)
    WebChart.ChartControl.PhysicalPath = Server.MapPath("/demos/")
    WebChart.ChartControl.VirtualPath = "/demos/"

    If Not Page.IsPostBack then
      'STEP 1: Get the data from the database
      Dim myConnection as New SqlConnection(Connection String)
      Const strSQL as String = "SELECT TOP 6 Name, ViewCount " & _
                               "FROM tblFAQCategory ORDER BY NEWID()"
      Dim myCommand as New SqlCommand(strSQL, myConnection)

      Dim reader as SqlDataReader = myCommand.ExecuteReader()      
      'STEP 2: Create the chart object
      Dim chart as New PieChart

      'STEP 3: Bind the DataTable to the WebChart
      chart.DataSource = reader
      chart.DataXValueField = "Name"
      chart.DataYValueField = "ViewCount"
      chart.DataLabels.Visible = True
      'STEP 4: Attach the chart object to the chart container
    End If      
  End Sub


<web:chartcontrol runat="server" id="ChartControl1"
         height="400" width="350" gridlines="none" legend-position="Bottom" />

[Return to the article...]