Creare un gestionale per strutture ricettive in C# – #6 – Caricamento Camere

Creare un gestionale per strutture ricettive in C# – #6 – Caricamento Camere

Creare un gestionale per strutture ricettive in C# – #6 – Caricamento Camere: Dopo aver collegato il database al nostro programma ed aver inviato i primi dati andiamo a caricare e mostrare i dati inseriti nella DataGridView.

In questa lezione infatti andremo a leggere i dati presenti nel database e li inseriremo nella DataGridView.

Inoltre andremo a contare quanti elementi sono presenti nella DataGridView e mostreremo il numero in basso.

Creare un gestionale per strutture ricettive in C# – #6 – Caricamento Camere

Il metodo che andremo a scrivere in questa lezione per permetterci di leggere tutte le informazioni dal database ed inviarle alla DataGridView è:

private async void CaricaDatabaseCamere()
        {
            dataElencoCamere.Rows.Clear();
            //CONNESSIONE DATABASE
            string stringa_connessione = Properties.Settings.Default.CamereConnectionString;
            SqlConnection connessioneDB = new SqlConnection(stringa_connessione);
            string sqlQuery = "SELECT * FROM [tblCamere]";

            using (connessioneDB)
            {
                try
                {
                    await connessioneDB.OpenAsync();
                    var InviaQuery = new SqlCommand(sqlQuery, connessioneDB);
                    SqlDataReader DatiTabellaCamere = await InviaQuery.ExecuteReaderAsync();
                    while (DatiTabellaCamere.Read())
                    {
                        string valoreId = DatiTabellaCamere.GetString(0);
                        string valoreNome = DatiTabellaCamere.GetString(1);
                        string valoreTipoCamera = DatiTabellaCamere.GetString(2);
                        int valorePostiLetto = DatiTabellaCamere.GetInt32(3);
                        int valoreLettiMatrimoniali = DatiTabellaCamere.GetInt32(4);
                        int valoreLettiSingoli = DatiTabellaCamere.GetInt32(5);
                        double valorePrezzo = DatiTabellaCamere.GetDouble(6);
                        int IntBagnoInCamera = DatiTabellaCamere.GetInt32(7);
                        bool valoreBagnoInCamera;
                        if (IntBagnoInCamera == 0){valoreBagnoInCamera = false;}else { valoreBagnoInCamera = true; }
                        string valoreNote = DatiTabellaCamere.GetString(8);
                        string[] NuovaCamera = { valoreId, valoreNome, valoreTipoCamera, valorePostiLetto.ToString(), valoreLettiMatrimoniali.ToString(), valoreLettiSingoli.ToString(),
                        valorePrezzo.ToString(), valoreBagnoInCamera.ToString(), valoreNote};
                        dataElencoCamere.Rows.Add(NuovaCamera);
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally {
                    if (connessioneDB.State == ConnectionState.Open)
                    {
                        connessioneDB.Close();
                    }
                    lblNumeroTotale.Text = dataElencoCamere.Rows.Count.ToString();
                }
            }