You are scrolling through each Datatable row within the foreach, only having to inform the column. You also cannot have return, return1 and return2 to get the results. Imagine that you will have 50 lines, will declare 50 variables ?
Use a list to store each value you’ve returned.
The use of Dataset / Datatable / Datarow is as follows:
DataSet.Tables[t]
where t is the table name or index.
DataSet.Tables[t].Rows[r]
where r is the line index.
DataSet.Tables[t].Rows[r][c]
where c is the column name or index.
DataSet dtteste = conexao.getDataSet("SELECT ID_FUNCIONARIO FROM VW_APONTAMENTO_HORAS WHERE ID_PROJETO="+idProjeto+"GROUP BY ID_FUNCIONARIO");
var count = dtteste.Tables[0].Rows.Count;
List<string> retornos = new List<string>();
foreach(DataRow teste in dtteste.Tables[0].Rows )
{
retornos.Add(teste[0].ToString());
}
Edit
Understanding that you first select the employees, and then at some point will select only the 'appointment_hours' for that employee, it should be something like yours Select
SELECT
ID_APONTAMENTO_HORAS,
Projeto,
Ativ,
Local,
[Dt Inic],
[Dt Fim]
FROM VW_APONTAMENTO_HORAS
WHERE ID_PROJETO = " + idProjeto +" AND
ID_FUNCIONARIO= "+ retornos[0] +";";
Back to your foreach
if you need a table for each employee, do so:
DataSet dtteste = conexao.getDataSet("SELECT ID_FUNCIONARIO FROM VW_APONTAMENTO_HORAS WHERE ID_PROJETO="+idProjeto+"GROUP BY ID_FUNCIONARIO");
var count = dtteste.Tables[0].Rows.Count;
string SqlApontamentos = "";
foreach(DataRow teste in dtteste.Tables[0].Rows )
{
SqlApontamentos += @"SELECT
ID_APONTAMENTO_HORAS,
Projeto,
Ativ,
Local,
[Dt Inic],
[Dt Fim]
FROM VW_APONTAMENTO_HORAS
WHERE ID_PROJETO = " + idProjeto +" AND
ID_FUNCIONARIO= "+ teste[0].ToString() +"; ";
}
DataSet dsApontamentos = conexao.getDataSet(SqlApontamentos);
And then on your dataset you’ll have n
tables where:
dsApontamentos.Tables[0]
is the first, dsApontamentos.Tables[1]
the second... etc
Wouldn’t be the variable
teste
within theforeach
?– novic
Voce was right even I was confused
– Flavio Ss
added var retorno4 = test[0]. Tostring(); and the first value appeared , plus the second value I can’t access , if I change the "[1]", it goes to the column and n I can put var retorno4 = test[0]. Rows[1]Tostring(); some suggestion .... more anyway I already appreciate the first help
– Flavio Ss
in your SQL does not have the second field! only has Id_work!
– novic
Just recommending a reading: Why Avoid Datatable and Dataset.
– Thiago Lunardi