MySQL Connector/NET Question

MrLucky

Well-known member
Joined
Mar 5, 2006
Messages
47
Ok, I have this piece of code:

C#:
		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:

C#:
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:
C#:
reader.GetValue(reader.GetOrdinal("total_played"));

but that resulted in a null exception.

Does anyone know how I can access that field?
 
Back
Top