0
I need an appeal similar to the one made 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().
So I tried to declare the array with the number of records:
email[] ListaEmail = new email[nRegistros];
and fill in the fields for each record. However, each element of Listemail is created with null content, not with the structure and error occurs at runtime when attempting to assign 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
{
}
}
Certainly not the most efficient way to solve the problem. However, as the control will not have very intense use and I have very little time to solve the problem, I tried this way. Have any suggestions to solve this problem?
Thank you!
Paul
Your list is not a list, so it does not accept the method
Add
.email ListaEmail = new email();
is wrong, should be:IList<email> ListaEmail = new List<email>();
– Gabriel Katakura