Problem with getting a custom named field..
Posted by: Lucas van Dijk
Date: March 05, 2006 08:36AM

I've 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 = Convert.ToInt32(reader.GetValue(reader.GetOrdinal("total_played")));
							legends = 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'm quering this 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 the problem is, How can i get the value of total_played?
I tried:
int total_played = Convert.ToInt32(reader.GetValue(reader.GetOrdinal("total_played")));
But that resulted in a null exception..

Does anyone know how to fix this?

Options: ReplyQuote


Subject
Written By
Posted
Problem with getting a custom named field..
March 05, 2006 08:36AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.