Solved

Charts in VB.Net

Posted on 2010-09-15
9
1,160 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now