Solved

Charts in VB.Net

Posted on 2010-09-15
9
1,198 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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