I need a feature similar to the one below:
email ListaEmail = new email();
…
while …
{
email Email = new email();
Email.email1 = adoDR["email"].ToString();
Email.tipo = adoDR["tipo"].ToString();
ListaEmail.Add(Email);
}
However, the list does not accept the Add () method.
Then I tried declaring the array with the number of records:
email[] ListaEmail = new email[nRegistros];
and fill in the fields of each record. However, each element of the E-mail List is created with null content, not with the structure, and run-time error occurs when trying to assign the content
ListaEmail [Cont].email1 = adoDR["email"].ToString();
The code is below:
public static email[] Func_RelatorEmail(Guid IdRelator)
{
try
{
string sql = "SELECT Relator_Email.IdRelator, Relator_Email.EmailId, Email.email, Email.tipo, Email.tipoSpecified "
+ "FROM Relator_Email INNER JOIN Email ON Relator_Email.EmailId = Email.EmailId "
+ "WHERE(Relator_Email.IdRelator='" + IdRelator + "')";
string connString = ClsParametros.Conexao;
SqlConnection adoConn = new SqlConnection(connString);
adoConn.Open();
SqlCommand adoCmd = new SqlCommand(sql, adoConn);
SqlDataReader adoDR = adoCmd.ExecuteReader();
// contar o numero de registros
int nRegistros = 0;
while (adoDR.Read())
{ ++nRegistros; }
// criar o objeto com o numero de registros
email[] ListaEmail = new email[nRegistros];
if (nRegistros > 0)
{
adoDR.Close();
adoDR = adoCmd.ExecuteReader();
int Cont = 0;
while (adoDR.Read())
{
ListaEmail[Cont].email1 = adoDR["email"].ToString();
ListaEmail[Cont].tipo = adoDR["tipo"].ToString();
Cont++;
}
}
adoDR.Close();
adoDR.Dispose();
adoCmd.Dispose();
adoConn.Close();
adoConn.Dispose();
return ListaEmail;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
}
}
Sure enough, it's not the most efficient way to solve the problem. However, as the control will not have very intensive use and I have very little time to solve the problem, I tried that way. Do you have any suggestions for resolving this issue?
Thank you!
Paulo