0

Hi i need select data from my database and then display these data in view in google chart. But i have no idea how to do it. Please can you help me ?

Model:

 public class Advert
    {

        public int id_reklamy { get; set; }
        public string nazev_reklamy { get; set; }
        public string typ_reklamy { get; set; }
        public int cena_reklamy { get; set; }
        public string email_uzivatele { get; set; }
        public string datumz { get; set; }
        public string datumk { get; set; }

}

Selecting data from my database:

  public List<Advert> SelectAllWithYear(string email_uzivatele, string datumz)
        {



            string queryString = "SELECT * from Reklama where 
(email_uzivatele like '%" + @email_uzivatele + "%') AND (datumz like '%" + @datumz + "%');";
            // Create the Command and Parameter objects.
            SqlCommand command = new SqlCommand(queryString, Connection);

            command.Parameters.AddWithValue("@email_uzivatele", "");
            command.Parameters.AddWithValue("@datumz", "");
            // Open the connection in a try/catch block. 
            // Create and execute the DataReader, writing the result
            // set to the console window.
            try
            {

                SqlDataReader reader = command.ExecuteReader();
                List<Advert> advert = new List<Advert>();
                while (reader.Read())
                {

                    Advert a = new Advert();
                    a.id_reklamy = int.Parse(reader[0].ToString());
                    a.nazev_reklamy = reader[1].ToString();
                    a.typ_reklamy = reader[2].ToString();
                    a.cena_reklamy = int.Parse(reader[3].ToString());
                    a.email_uzivatele = reader[4].ToString();
                    a.datumz = reader[5].ToString();
                    a.datumk = reader[6].ToString();



                    Console.WriteLine("id: " + a.id_reklamy + " " + "nazev_reklamy: " + " "
                        + a.nazev_reklamy + " " + "typ_reklamy: " + " " +
                      a.typ_reklamy + " cena_reklamy: " + " " + a.cena_reklamy +
                      " email_uzivatele: " + " " + a.email_uzivatele + " datum_zacatku : " +
                      " " + a.datumz + " datum_konce : " + " " + a.datumk);

                    advert.Add(a);
                }

                reader.Close();
                return advert;
            }
            catch (Exception ex)
            {
                chyba.zapsat_do_souboru(ex.Message);
                Console.OpenStandardOutput();
                Console.WriteLine(ex);
                //zalogovat chybu
                return null;
            }


        }

Controller:

        [HttpGet]
        public ActionResult Chart()
        {
            AdvertModel objAdvertModel = new AdvertModel();
            objAdvertModel.typ_reklamy = "Typ reklamy";
            objAdvertModel.cena_reklamy = "Cena reklamy";
            return View(objAdvertModel);
        }

        public ActionResult GetChart(string email_uzivatele, string datumz)
{
    return Json(AdvertServiceLayer.Instance.SelectAllWithYear(email_uzivatele, datumz)

            ,JsonRequestBehavior.AllowGet);
}

and view :

@{
    ViewBag.Title = "Charts";
}

<h2>Charts</h2>

<script>
    function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Visitations', { role: 'style' } ],
          ['2010', 10, 'color: gray'],
          ['2010', 14, 'color: #76A7FA'],
          ['2020', 16, 'opacity: 0.2'],
          ['2040', 22, 'stroke-color: #703593; stroke-width: 4; fill-color: #C5A5CF'],
          ['2040', 28, 'stroke-color: #871B47; stroke-opacity: 0.6; stroke-width: 8; fill-color: #BC5679; fill-opacity: 0.2']
        ]);//insted of hard coded data i want here selected data from databse
</script>
Jakub Bocek
  • 47
  • 1
  • 1
  • 9
  • But this is not solving my problem i dont know how to pass data from database into js to drawChart – Jakub Bocek Mar 25 '16 at 11:32
  • But this mvc asp.net not php – Jakub Bocek Mar 25 '16 at 12:10
  • Thanks but i found this similar question http://stackoverflow.com/questions/32524745/how-to-populate-google-charts-using-data-from-database-created-via-code-first but he is using code first and i dont know how can i use this in my case ? Any idea ? – Jakub Bocek Mar 25 '16 at 17:52

0 Answers0