Solved

Charts in VB.Net

Posted on 2010-09-15
9
1,180 Views
Last Modified: 2013-11-26
I know I'm pretty close to having this correct, but it's eating more time than I'd like to spend on it, so here I am, asking my trusty EE friends.

I have a simple CSV file which contains data such as:


Neil Crecelius,2965,2855
Loraine Vass,2964,3104
Cody Rothwell,2959,2689
Rae Mccarney,2937,2744
Margery Mcadam,2926,3065

This data represents Name, Total for May 2010, Total for April 2010 (the previous month)

I am trying to chart this using the new chart controls in VB2010, using a line.  One line would show data for one month, and the other line would show the previous month.

Here is the code.  I can get one month, or the other to show, but not both at the same time.  I am sure it's something silly, but the day is almost over, and I can't wake up enough to figure  it out.

In the lines where you see (myReader, 1, myReader, 2) etc, I was assuming that those numbers were representing the column number in the CSV file.
' Full path to the data source file
        Dim file As String = "Top5.csv"

        ' Create a connection string.
        Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:/gatewaydata/" + ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
        Dim myConnection As New OleDbConnection(ConStr)

        ' Create a database command on the connection using query
        Dim mySelectQuery As String = "Select * from " + file
        Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

        ' Open the connection and create the reader
        myCommand.Connection.Open()
        Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

        ' databind the reader to the chart using the DataBindXY method
        Chart1.Series(0).Points.DataBindXY(myReader, 1, myReader, 2)
        Chart1.Series(1).Points.DataBindXY(myReader, 1, myReader, 3)

        ' Close connection and data reader
        myReader.Close()
        myConnection.Close()

Open in new window

0
Comment
Question by:LD147
  • 5
  • 4
9 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 33688012
Try using DataView instead of DataReader. Load the data in a DataSet and then use its DataTable.DefaultView.
0
 
LVL 1

Author Comment

by:LD147
ID: 33694081
I'll try that as a last resort, but I'd like to try to get the above code fixed first.  It seems like the first series is being drawn ok, and then the second series overwrites it and removes the first.  I'd like them both to appear together.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 33697963
I believe the issue is because of the DataReader and that is why you should try the DataTable. The DataReader object can be traversed only once while the DataView can be traversed multiple times. If the Chart need to traverse the DataReader twice in the second traversal, it won't find anything and that may be why only one series shows up.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:LD147
ID: 33737995
To be honest, the code above is copied/pasted from somewhere....I don't know how to write that kind of stuff myself, and I know I don't know how to use a DataView, or make a DataSet.  Do you have some sample code?  
0
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 33750871
       ' Full path to the data source file
        Dim file As String = "Top5.csv"

        ' Create a connection string.
        Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:/gatewaydata/" + ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
        Dim myConnection As New OleDbConnection(ConStr)

      myConnection.Open()

        ' Create a database command on the connection using query
        Dim mySelectQuery As String = "Select * from " + file
        Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

      Dim myDataSet As DataSet
      Dim adapter As New OleDbDataAdapter
      adapter.SelectCommand = new OleDbCommand(mySelectQuery, myConnection)
      adapter.Fill(myDataSet)

        ' databind the reader to the chart using the DataBindXY method
        Chart1.Series(0).Points.DataBindXY(myDataSet.Tables(0).DefaultView, 1, myDataSet.Tables(0).DefaultView, 2)
        Chart1.Series(1).Points.DataBindXY(myDataSet.Tables(0).DefaultView, 1, myDataSet.Tables(0).DefaultView, 3)

        ' Close connection
        myConnection.Close()

0
 
LVL 1

Author Comment

by:LD147
ID: 33769173
Thanks.  The line ' adapter.Fill(myDataSet)' is giving me a warning:

Warning      1      Variable 'myDataSet' is used before it has been assigned a value. A null reference exception could result at runtime.

When the chart comes up, it shows a blank chart (nothing on it), so I don't know if the parameters are incorrect or not.  See pic.






Capture.JPG
0
 
LVL 58

Expert Comment

by:amit_g
ID: 33852068
Add a line before the warning line

myDataSet = New DataSet()
0
 
LVL 1

Author Comment

by:LD147
ID: 33877395
Thank you Amit!  That worked perfectly :)
0
 
LVL 1

Author Closing Comment

by:LD147
ID: 33877422
Amit solved the problem! :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question