Solved

Charts in VB.Net

Posted on 2010-09-15
9
1,177 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now