CaioVJesus89
CaioVJesus89

Reputation: 333

Foreach SQLDataReader

I have an page that list rows from my DB table, with User Name and an CheckBox. I need an function that send email to User when CheckBox.checked is true, but don't show Email in my page. I have these mothods:

public class ListRequest
{
    public string UserName { get; set; }
    public string Email { get; set; }
}

public List<ListRequest> PreencheValores(SqlDataReader reader)
{
    var lista = new List<ListRequest>();
    while (reader.Read())
    {
        var listRequest = new ListRequest();
        listRequest.UserName = reader["User"].ToString();
        listRequest.Email = reader["Email"].ToString();
        lista.Add(listRequest);
    }
    return lista;
}

public List<ListRequest> ConsultarRequest()
{
    var lstRetorno = new List<ListRequest>();
    using (objConexao = new SqlConnection(strStringConexao))
    {
        using (objCommand = new SqlCommand(strSelectPorID, objConexao))
        {
            try
            {
                objConexao.Open();
                objCommand.Parameters.AddWithValue("@UserName", dm3);

                var objDataReader = objCommand.ExecuteReader();
                if (objDataReader.HasRows)
                    lstRetorno = PreencheValores(objDataReader);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                objConexao.Close();
            }
        }
    }
    return lstRetorno;
}

How I can make foreach for this?? I need get all string Email to this list but don't show Email in my page. now I use foreach (GridViewRow row in GridView.Rows) to get name when CheckBox.cheked is true. I'm clear in question? Tks!

Upvotes: 1

Views: 2787

Answers (1)

KennyZ
KennyZ

Reputation: 907

If you want to do something with each row in objDataReader, use

While objDataReader.Read()
    'do something
End While

Upvotes: 1

Related Questions