title: How to get the results in a while loop in order to use it as a return in a function, c#
body: <p>I'm trying to build a class with all my functions on it.</p>
  
  <p>My function is trying to return my variable results which is a type TimeSpan. But I can't get the dataRecupere out of the condition in order to use it for my result.</p>
  
  <p>Here is my code : </p>
  
  <pre><code>public TimeSpan differenceDate(string idAdmission)
          {
              TimeSpan resultat;
              TimeSpan dateRecupere;
              string requete = "SELECT AdmitDate FROM AdmissionRecords WHERE AdmissionID = @idAdmission";
              connexion.Open();
  
              SqlCommand commande = new SqlCommand(requete, connexion);
              commande.Parameters.AddWithValue("@idAdmission", idAdmission);
              SqlDataReader lecteur = commande.ExecuteReader();
              if (lecteur.HasRows)
              {
                  lecteur.Read();
                  dateRecupere = TimeSpan.Parse((lecteur["AdmitDate"]).ToString());
  
              }            
              connexion.Close();
              return resultat = dateRecupere - DateTime.Now.TimeOfDay;
          }
  </code></pre>