ASP.NET - Bind Chart control from database - Asked By Nitish Gupta on 13-Sep-12 08:53 AM

 I am using chart control in 4.0 and i want to bind data to chart control from database table.
I want to show performance (good,average,excellent) on Y-axis and Test topic(test1,test2) in X-axis from table.
tell me the source code.
Danasegarane Arunachalam replied to Nitish Gupta on 13-Sep-12 10:39 AM
Refer Using Microsoft's Chart Controls from
Jitendra Faye replied to Nitish Gupta on 14-Sep-12 12:06 AM

Follow this code-

using System.Data.OleDb;
using System.Data;
using System.Web.UI.DataVisualization.Charting;

// Resolve the address to the Access database. We assume database is
// in Bin folder.
string fileNameString = "chartdata.mdb";

// Initialize a connection string.
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;

// Define the database query.
string mySelectQuery="SELECT * FROM SALESCOUNTS;";

// Create a database connection object using the connection string.
OleDbConnection myConnection = new OleDbConnection(myConnectionString);

// Create a database command on the connection using query.
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);

// Open the connection.

// Create a database reader.
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

// Specify the Name column to be used for point's X values.

// Close the connection.

//  This is a loop to set all created charts appearance with custom attribute.
foreach (Series series in chart1.Series)
    series.CustomAttributes = "DrawingStyle=LightToDark";