Perform a select and return the emails in a list

1

My question is this: I am studying C# and I am working on a project (study only) of student registration. I made a form to send e-mail if the student misses the school, except that I did not implement the faults column and I am doing the test with the school year. I make select email from Cadastro where anoletivo = 2010 . I registered about 5 students with different emails and with the 2010 school year. Only, I wanted to perform this select, store the emails in a list so I can pass this list to my method of sending e- mails. How could I do that?

Code:

CLASSE DAL

       public List<ModeloAniversariante> CarregaModeloAniversariante()
        {
            List<ModeloAniversariante> lst = new List<ModeloAniversariante>();
            try
            {

                ModeloAniversariante modelo = new ModeloAniversariante();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conexao.ObjetoConexao;
                cmd.CommandText = "select email from Cadastro where anoletivo = 2010";
                conexao.Conectar();
                SqlDataReader registro = cmd.ExecuteReader();
               while (registro.Read())
               {
                   modelo.EmailResponsavel = Convert.ToString(registro["email"]);

                    lst.Add(modelo);
               }
                return lst;
            }
            catch (SqlException er)
            {
                throw new Exception(er.Message);
            }
            finally
            {
                conexao.Desconectar();
            }
        }

MODELO

    public class ModeloAniversariante
    {
        private List<string> _emailresponsavel;

        public List<string> EmailResponsavel
        {
            get { return this._emailresponsavel; }
            set { this._emailresponsavel = value; }

        }

NO "BOTÃO"

        private void enviarEmailAosResponsáveisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DALConexao cx = new DALConexao(DadosDaConexao.StringDeConexao);
            BLLAniversariante bll = new BLLAniversariante(cx);

            ModeloAniversariante modelo = bll.CarregaModeloAniversariante(3);
            string responsavel = Convert.ToString(modelo.EmailResponsavel);

            Email = new MailMessage();
            Email.To.Add(new MailAddress(responsavel));
            Email.From = (new MailAddress("[email protected]"));
            Email.Subject = "Teste";
            Email.IsBodyHtml = true;
            Email.Body = "Isso é um teste";
            SmtpClient cliente = new SmtpClient("smtp.live.com", 587);
            using (cliente)
            {
                cliente.Credentials = new System.Net.NetworkCredential("[email protected]", "minhasenha");
                cliente.EnableSsl = true;
                cliente.Send(Email);
            }
            MessageBox.Show("E-mail enviado com sucesso!", "Sucesso");
        }
    
asked by anonymous 27.02.2016 / 19:06

1 answer

1

Well, if you put this list together smoothly:

List<ModeloAniversariante> lst = new List<ModeloAniversariante>();

And you have successfully entered the result of your selection into it:

lst.Add(modelo);

There is not much secret. Just need to iterate through the result:

    // Isto aqui está errado. Você está devolvendo uma lista. 
    // Não um elemento apenas.
    // ModeloAniversariante modelo = bll.CarregaModeloAniversariante(3);

    // O correto é:
    List<ModeloAniversariante> modelos = bll.CarregaModeloAniversariante();
    // Se é uma lista, você precisa iterar essa lista, assim:
    foreach (var modelo in modelos)
    {
        // Isto não precisa. EmailResponsavel já é string.
        // string responsavel = Convert.ToString(modelo.EmailResponsavel);

        Email = new MailMessage();
        Email.To.Add(new MailAddress(modelo.EmailResponsavel));
        Email.From = (new MailAddress("[email protected]"));
        Email.Subject = "Teste";
        Email.IsBodyHtml = true;
        Email.Body = "Isso é um teste";
        SmtpClient cliente = new SmtpClient("smtp.live.com", 587);
        using (cliente)
        {
            cliente.Credentials = new System.Net.NetworkCredential("[email protected]", "minhasenha");
            cliente.EnableSsl = true;
            cliente.Send(Email);
        }

        MessageBox.Show("E-mail enviado com sucesso!", "Sucesso");
    }

About this here:

modelo.EmailResponsavel = Convert.ToString(registro["email"]);

Use:

modelo.EmailResponsavel = Convert.ToString(registro.GetValue(registro.GetOrdinal("email")));
    
27.02.2016 / 19:35