Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ok, I have this piece of code:

 

	private void CreateMostPlayedChart()
	{
		PieChart chart = new PieChart();
		MySqlConnection conn = new MySqlConnection();
		MySqlCommand command = new MySqlCommand();
		MySqlDataReader reader;
		
		//
		// Build legend and value array
		//			
		conn.ConnectionString = this.conn_string;
		
		try
		{
			conn.Open();
			
			try
			{
				command.Connection = conn;
				command.CommandText = "SELECT COUNT(history_id) AS total_played, history_artist, history_title, history_date " +
				"FROM winamp_history " +
				"GROUP BY history_artist, history_title " +
				"ORDER BY total_played DESC " +
				"LIMIT 0, 10";
				
				reader = command.ExecuteReader();
				
				if(reader.HasRows)
				{
					int[] values = null;
					string[] legends= null;
					
					int i = 0;
					while(reader.Read())
					{
						values[i] = Convert.ToInt32(reader.GetValue(reader.GetOrdinal("total_played")));
						legends[i] = reader.GetValue(reader.GetOrdinal("history_artist")) + " - " + reader.GetValue(reader.GetOrdinal("history_title"));
					}

					chart.SetValues(values);
					chart.SetLegends(legends);
					chart.Render("Most played songs", "", 200, 200);

					System.Drawing.Image final = chart.Final;

					graphMostPlayed.Image = final;
				}
			}
			catch(MySqlException ex)
			{
				MessageBox.Show("Could not get history: " + ex.Message + "\n\n" + command.CommandText);
			}
		}
		catch(MySqlException ex)
		{
			MessageBox.Show("Could not connect to db: "+ ex.Message);
		}			
	}

 

As you can see, I execute this SQL query:

 

command.CommandText = "SELECT COUNT(history_id) AS total_played, history_artist, history_title, history_date " +
				"FROM winamp_history " +
				"GROUP BY history_artist, history_title " +
				"ORDER BY total_played DESC " +
				"LIMIT 0, 10";

 

But my quetion is, How can I access 'total_played'? I tried to use:

reader.GetValue(reader.GetOrdinal("total_played"));

 

but that resulted in a null exception.

 

Does anyone know how I can access that field?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...