Store values from a data set in ASP.NET variables

0

I'm having problems getting the return of a select variable, I'm using a data set to create a table with the result of select, that select will return some records and form that I'm doing just get the first value, I already researched how to do list and to go using for each more I am not able to hear in practice here goes a piece of the code

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;
foreach(DataRow teste in dtteste.Tables[0].Rows )
{

   var retorno = dtteste.Tables[0].Rows[0].ToString();
   var retorno1 = dtteste.Tables[0].Rows[2].ToString();
   var retorno3 = dtteste.Tables[0].Rows[4].ToString();
}

In this way it does not show any results, I think I need to convert from SQL to int or something like this?

    
asked by anonymous 28.06.2017 / 21:00

1 answer

0

You are traversing each row of the DataTable within the foreach, so you only have to enter the column. You also can not have return, return1 and return2 to pick up the results. Imagine you will have 50 lines, will you declare 50 variables? Use a list to store each value you have returned.

The use of DataSet / DataTable / DataRow is as follows:

DataSet.Tables[t] where t is the name or index of the table.

DataSet.Tables[t].Rows[r] where r is the index of the line.

DataSet.Tables[t].Rows[r][c] where c is the name or index of the column.

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 you will select only the 'appointments' for that employee, it should be something like this 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] +";";

Let's get back to your foreach if you need a table for each id_function, 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 in your DataSet you will have n tables where:

dsApontamentos.Tables[0] is the first, dsApontamentos.Tables[1] to second ... etc

    
28.06.2017 / 21:11