.NET 4.0 Chart Control Problem – Connection Was Not Closed


So, I’ve go this huge perfmon log file that I’m trying to get some charts out of and excel just isn’t cutting it for me and SSRS just turned out to be a pain and no easier than making my own app (learning curve) so here I am, making a basic charting program in C#. As you may have noticed I spend most of my time in Powershell so I’m stumbling over this a bit and I ran in to a problem using the charting control that is telling me the connection was not closed when I attempt to bind my SQLCommand object to the chart control. Come to find out the connection needs to be closed and the chart needs to open it.

here is some sample code:

         

          string query = "select field1, field2 from table";
          string connstr = "Server=yourserver;Database=yourdb;integrated security";
          SqlConnection conn = new SqlConnection(connstr);
          SqlCommand cmd = new SqlCommand(query, conn);
          
          try
          {
              chart1.DataSource = cmd;
              chart1.Series["Series1"].XValueMember = "field1";
              chart1.Series["Series1"].YValueMembers = "field2";
          }
          catch (Exception Ex)
          {
              MessageBox.Show(Ex.Message);
          }

 

 

As you’ll notice, no where do you open or close the connection, its handled by the chart. Seems a little weird to me, but I guess that’s cool.

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on September 23, 2010, in .NET and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: