Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a pie chart that updates whenever the values of the database it is connected to are changed. But everything I tried so far is not making it happen. Any suggestions on what I should do?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace HCI_Application
{
    public partial class Nutritional : Form
    {
        int carb = 0;
        int protein = 0;
        int fat = 0;
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Event Driven Programming\\HCI Application\\Nutrients.accdb");
        public Nutritional()
        {
            InitializeComponent();
        }

        private void chart1_Click(object sender, EventArgs e)
        {
        }

        private void Nutritional_Load(object sender, EventArgs e)
        {
            int maintenance = Convert.ToInt32(CalorieCalculator.intake);
            int fatloss = maintenance - 500;
            int gain = maintenance + 500;

            lblIntake.Text = "BMR: " + CalorieCalculator.intake.ToString();
            lblMaintenance.Text = "Maintenance: " + maintenance.ToString();
            lblFatloss.Text = "Fat Loss: " + fatloss.ToString();
            lblGain.Text = "Weight Gain: " + gain.ToString();

            this.nutrientsTableAdapter.Fill(this.nutrientsDataSet.Nutrients);
            cmbDiet.Items.Add("Standard Diet (60-30-10)");
            cmbDiet.Items.Add("Ketogenic Diet (10-45-45)");
            cmbDiet.Items.Add("Bodybuilding Diet (50-30-20)");
            cmbDiet.Items.Add("High Protein Diet (25-50-25)");
            cmbDiet.Items.Add("Zone Diet (40-30-30)");
            cmbDiet.SelectedIndex = 0;    
        }

        private void cmbDiet_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbDiet.SelectedIndex)
            {
                case 0:
                    carb = 60;
                    protein = 30;
                    fat = 10;
                    break;
                case 1:
                    carb = 10;
                    protein = 45;
                    fat = 45;
                    break;
                case 2:
                    carb = 50;
                    protein = 30;
                    fat = 20;
                    break;
                case 3:
                    carb = 25;
                    protein = 50;
                    fat = 25;
                    break;
                case 4:
                    carb = 40;
                    protein = 30;
                    fat = 30;
                    break;  
            }

            connection.Open();

            OleDbCommand updateValues = new OleDbCommand("UPDATE Nutrients SET Percentage='" + carb + "' WHERE Nutrient = 'Carbohydrates'", connection);
            updateValues.ExecuteNonQuery();

            updateValues.CommandText = "UPDATE Nutrients SET Percentage='" + protein + "' WHERE Nutrient = 'Protein'";
            updateValues.ExecuteNonQuery();

            updateValues.CommandText= "UPDATE Nutrients SET Percentage='" + fat + "' WHERE Nutrient = 'Fat'";
            updateValues.ExecuteNonQuery();

            DrawChart();
            connection.Close();
        }

        private void DrawChart()
        {
            DietChart.Series["NutrientSeries"].XValueMember = "Nutrient";
            DietChart.Series["NutrientSeries"].YValueMembers = "Percentage";
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 25-Nov-12 11:24am    
Writing code is so heavily hard-coded manner (just look at your switch block), how can you hope for updating of anything, ever?
--SA
codeninja-C# 28-Nov-12 4:02am    
Hi Sergey,
Where can i use switch block? How can i use it more effectively? Can you provide a small example to reconstruct above switch block
Thanks
--SRJ
Sergey Alexandrovich Kryukov 28-Nov-12 11:26am    
Bad idea, in general. Cases values are non-semantic. At least, you could define enumeration, to show what they mean and avoid error. Ideally, it should be one line getting those three values from some data collection you could have in file or somewhere... No need for switch at all...
Another problem is that you tend to work with strings representing data instead of data.

You are not quite ready yes; it you don't see how bad it is, you simply don't know yet hot to use language and API. Learn the whole language and essential part of .NET...
--SA
codeninja-C# 28-Nov-12 23:21pm    
Hi Sergey,
thanks for your valuable inputs
--SJ
Sergey Alexandrovich Kryukov 29-Nov-12 0:35am    
You are welcome. I wish I could help more, but here some more work is required...
--SA

1 solution

In WPF you have observable collection by using that each change on data reflects in UI automatically >:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900