C# VS2008 charting example error
After downloading and installing the Windows Forms Chart Control for VS2008 , I have been trying to learn the using the built-in examples.
One of the examples is for data binding and charting a CSV file.
Unfortunately, although all looks OK. I keep getting "Index was out of range..." error for:
"chart1.Series[0].Points.DataBindXY(myReader, "1", "2");".
I have checked the myReader object, and it seems fine, with all data columns and rows in position, yet I get that error.
Here is the full code: (all copied from the MS example, path modified)
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data.OleDb;
using System.Data;
namespace Charting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
createCSVChart();
}
public void createCSVChart()
{
Chart chart1 = new Chart();
// Create Chart Area
ChartArea chartArea1 = new ChartArea();
// Add Chart Area to the Chart
chart1.ChartAreas.Add(chartArea1);
// Full path to the data source file
string path = @"E:\Documents and Settings\sam\My Documents\Visual Studio 2008\Charting\data";
string file = "DataFile.csv";
// Create a connection string.
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
path + ";Extended Properties=\"Text;HDR=No;FMT=Delimited\"";
OleDbConnection myConnection = new OleDbConnection(ConStr);
// Create a database command on the connection using query
string mySelectQuery = "SELECT * FROM " + file;
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
// Open the connection and create the reader
myCommand.Connection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// Column 1 is a time value, column 2 is a double
// databind the reader to the chart using the DataBindXY method
chart1.Series[0].Points.DataBindXY(myReader, "1", "2"); // This shows Index error.
// Close connection and data reader
myReader.Close();
myConnection.Close();
}
}
}