Store data set values in ASP.NET variables

Asked

Viewed 721 times

0

I’m having trouble storing the return of a select in variables , I’m using a data set to create a table with the result of select , this select will return me some records and the form I’m making so I can get the first value , already researched how to make list and go through using for each more I’m not getting oir in practice here goes a piece of 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();
}

This way it does not present any result, I think I need to convert from SQL to int or something like?

  • Wouldn’t be the variable testewithin the foreach?

  • 1

    Voce was right even I was confused

  • 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

  • in your SQL does not have the second field! only has Id_work!

  • Just recommending a reading: Why Avoid Datatable and Dataset.

1 answer

0


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

  • more in this case where is stored my results , it seemed much simpler but n understood with clarity , I am new in this area , more I thank the help Rovan!!

  • was stored in return now I think I understood , very obliged

  • all values are within the List<string> retornos. The ideal is to know what the need to then know which resource to use. per hour, I recommend reading: http://www.linhadecodigo.com.br/artigo/3676/listt-workcom-generic lists-em-csharp.aspx

  • i would like to use this result with a select , but I think I have to first convert to string the return ne?? would be something like this

  • var teste1 = returns.Tostring(); global.Chargingtimes1(tbApontmentHoras, "SELECT ID_APONTAMENTO_HORAS, Projeto,Ativ,Local,[Dt Inic],[Dt Fim] FROM VW_APONTAMENTO_HORAS WHERE ID_PROJETO =" + idProject +"ID_FUNCIONARIO="+teste1.tostring());

  • it doesn’t make much sense for you to run a SELECT to with the result of it, just run another. I’ll put how the code would look in the answer. And, var teste1 = retornos.ToString(); is wrong, where returns is a List<>

  • List<string> returns = new List<string>(); foreach (Datarow test in dtteste. Tables[0]. Rows) ' returns.Add(test[0]. Tostring() string teste1 = returns[i]. Tostring(); global.Chargingappointmenttimes1(tbApplicationHoras, "SELECT ID_APONTAMENTO_HORAS, Projeto,Ativ,Local,[Dt Inic],[Dt Fim] FROM VW_APONTAMENTO_HORAS WHERE ID_PROJETO =" + idProjeto+"AND ID_FUNCIONARIO="+teste1); i = i + 1; }

  • I ended up doing this way , more really does not meet well what I need , it was for each result I load a table of notes , more this form it only carries the last id sent , I wanted to make for each result it creates a difernete table

  • Exactly that guy , you helped me more , Thank you Rovann!!!!

  • for nothing, if possible can evaluate the answer as +1 also =]

  • 1

    i tried rovann more talks that members with score less than 15 can not vote , as I made my registration yesterday still n I have this ranking , more once I have the points I vote beauty and Briado again

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.