Using FusionCharts with VB.NET (ASP.NET) > Plotting data from a database |
In this section, we'll show you how to use FusionCharts and ASP.NET to plot charts from data contained in a database. We'll create a pie chart to show "Production by Factory" using:
For the sake of ease, we'll use an Access Database. The database is present in Download Package > Code > VBNET > DB folder. You can, however, use any database with FusionCharts including MS SQL, Oracle, MySQL etc. Before you go further with this page, we recommend you to please see the previous section "Basic Examples" as we start off from concepts explained in that page. The code examples contained in this page are present in Download Package > Code > VBNET > DBExample folder. The Access database is present in Download Package > Code > VBNET > DB. |
Database Structure |
Before we code the ASP.NET pages to retrieve data, let's quickly have a look at the database structure. |
The database contains just 2 tables:
For demonstration, we've fed some dummy data in the database. Let's now shift our attention to the ASP.NET page that will interact with the database, fetch data and then render a chart. |
Building the ASP.NET Page for dataXML Method |
The ASP.NET page for dataXML method example is named as BasicDBExample.aspx (in DBExample folder). It contains the following code: |
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="BasicDBExample.aspx.vb" Inherits="DBExample_BasicDBExample" %> <HTML> <BODY> |
In the above code, we have included FusionCharts.js file to render chart through javascript.We are also adding an ASP control literal which acts as the container for the charts. The CreateCharts() function does the generation, and is the code behind the file, BasicDBExample.vb. Let's take a look at the code behind file: |
Imports DataConnection Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Generate chart in Literal Control Public Function CreateChart() As String 'Database Objects - Initialization 'Generate the graph element ' SQL Query ' Open Data Reader 'Generate <set name='..' value='..' /> ' Close Data Reader 'Create the chart - Pie 3D Chart with data from strXML End Function End Class |
The following actions are taking place in this code:
When you now run the code, you'll get an output as under: |
Converting the example to use dataURL method |
Let's now convert this example to use dataURL method. As previously explained, in dataURL mode, you need two pages:
The pages in this example are contained in Download Package > Code > CNET > DB_dataURL folder. |
Chart Container Page - Default.aspx |
Default.aspx contains the following code to render the chart: |
<%@ Page Language="VB" %> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) </script> <HTML> <asp:Literal ID="FCLiteral" runat="server"></asp:Literal> |
In the above code, we:s
|
Creating the data provider page PieData.aspx.vb |
PieData.aspx.vb contains the following code to output XML Data: |
Imports DataConnection
'For the sake of ease, we've used an Access database which is present in 'Database Objects - Initialization 'Generate the graph element ' SQL Query ' Open Data Reader 'Iterate through each factory End While 'Set Proper output content-type 'Just write out the XML data |
In the above page:
When you view this page, you'll get the same output as before. |
Inside DataConnection Namespace |
We have used DataConnection Namespace in the above code and in all subsequent Database examples. Using this class we establish connection to the MS Access database with ADO.NET component. Let's go through the lines of code inside this class: |
Imports Microsoft.VisualBasic Namespace DataConnection ''' <summary> ''' <summary> ' MS Access DataBase Connection - Defined in Web.Config '' SQL Server DataBase Connection - Defined in Web.Config ' Creating Connection string using web.config connection string ' Creating OdbcConnection Oject ' Setting Conection String ' Open Connection ' get reader Catch ex As Exception End Sub ' Create a Command object ' Create data reader object using strQuery string End Sub |
What it does:
|