Solved

Charts in VB.Net

Posted on 2010-09-15
9
1,212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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