title: C# - How can I declare a variable that will get populated by a database call inside an if statement?
body: <p>I want get data from a database call that's inside an if statement and then i want to be able to work with that data outside the if statement later in the page. </p>
  
  <p>So, why doesn't this work?  And what can I do to make this work?  I just get the error message "Compiler Error Message: CS0103: The name 'selectedData' does not exist in the current context".  I tried to declare a "selectedData" variable outside of the if statement so it would be available, but I can't seem to figure out how to do that properly.</p>
  
  <pre><code>@{
  
      var testVariable = "blah";
  
      //set cache key and query based their being a craft name
      if(testVariable.Length &gt; 0){
  
          var db = Database.Open("Connection"); 
          var selectedData = db.Query("SELECT * FROM Products");             
      } 
  
  }
  
  
  &lt;div&gt;
      @foreach (var row in selectedData){
          @row.ContentTitle &lt;br /&gt;
          @row.ContentShortDescription &lt;br /&gt;
      } 
  &lt;/div&gt;
  </code></pre>