I wanted to know how to connect the database data to the chart data in Windows Forms C#

Asked

Viewed 31 times

-1

I have no idea how I’m going to handle the data, so far what I’ve tried to accomplish is this code:

public void loadChart()
{
        MySqlConnection conexao = new MySqlConnection();
        conexao.ConnectionString = "Server=localhost;Database=db_softaurus;Uid=root;Pwd=usbw;";
        conexao.Open();
        string Query = "select genero_animal, Count(1) as Total  from tb_animal group by genero_animal";
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conexao);
        MySqlDataReader myReader;
        try
        {
            myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {
                chart1.Series["Macho"].Points.AddXY(myReader.GetString("genero_animal"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }`

I wanted to return the sex of the animal separated by male and female on the chart

  • I don’t understand, exactly, what is your doubt? what is the error presented?

  • So I wanted to display on the graph the amount of animals of the male gender and the female gender, but I have no idea how to do this, I tried to do with this code but, it didn’t work very well

  • You can start by changing your query, taking out the Where genero_animal = 'M' and replacing by select genero_animal, Count(1) as Total, from tb_animal Group By genero_animal

  • In the case here gave Syntax error

  • Is that now you need to change the rest of the code...

  • I got it. what I do now?

  • It depends, what did you do? Edit the question by updating your progress.

  • I changed the query, but I don’t know what I should do to be able to insert this query data in the chart

Show 3 more comments

1 answer

0

Taking into consideration the Enum below and that you want to display only the totals by gender, you can follow the example below.

public enum GeneroEnum
{
    Macho  = 'M',
    Femea = 'F'

}

Code

MySqlConnection conexao = new MySqlConnection();
conexao.ConnectionString = "Server=localhost;Database=db_softaurus;Uid=root;Pwd=usbw;";
conexao.Open();
string Query = "select genero_animal, Count(1) as total  from tb_animal group by genero_animal";
MySqlCommand cmdDataBase = new MySqlCommand(Query, conexao);
MySqlDataReader myReader;

try
{
    //remove as colunas pré configuradas ao adicionar o componente
    chart1.Series.Clear();

    myReader = cmdDataBase.ExecuteReader();
    while (myReader.Read())
    {
        //identificando o nome da coluna
        var serie = Enum.GetName(typeof(GeneroEnum), myReader.GetChar("genero_animal")).ToString();
        
        //adicionando a nova coluna
        chart1.Series.Add(serie);

        chart1.Series[serie].Points.AddXY(0, myReader.GetInt32("total"));

    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Example inserir a descrição da imagem aqui

  • It worked, thank you very much man!

  • @Enzodacostaxavier If the answer solved your problem, you should vote for it and accept it as the conclusion of the problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.