ASP.NET Web page 2, Query question...

I can get this to work when I don't have the second query, but when I do use the second query (foreach loop near bottom) to try to display additional data I get this message:

Compiler Error Message: CS0103: The name 'db' does not exist in the current context.  Why does the first query work and not the second?


@{

//member variables are here...
   
var fname = "";


 <.....snip....>




if(!IsPost){
    if(!Request.QueryString["MemberID"].IsEmpty() && Request.QueryString["MemberID"].IsInt()){
        MemberID = Request.QueryString["MemberID"];
        var db = Database.Open("MyDB");
        var dbCommand = "SELECT tblMembers.*, tblPractices.* FROM tblMembers, tblPractices WHERE tblMembers.PracticeID = tblPractices.PracticeID AND MemberID = @0 ORDER By tblMembers.Lname"; 
        var dbCommandPods = "SELECT tblPods.*, tblPodMembers.* FROM tblPodMembers INNER JOIN tblPods on tblPodMembers.PodId = tblPods.PodId WHERE MemberId = @0";
        var row = db.QuerySingle(dbCommand, MemberID);
                       

        if(row !=null){
            fname = row.fname;            
            //more here....

 		<.....snip....>

            }                     
        }
    }
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <h2>Detailed Member Information</h2>
            <div>
                 <h3>Basic Info</h3>
                  <table border="1">
                    <tr>
                        <td>Member Name: @fname @MInitial  @lname  @suffix</td>
                    </tr>
                    <tr>
                        <td>Practice Name: @name</td>                          
                    </tr>
                    <tr>
                        <td>Date of Birth: @dob</td>                          
                    </tr>
                    <tr>                          
                    </tr>
                    <tr>
                        <td>Email Address: @email</td>
                    </tr>
                  </table>              
            </div>
            
           
           
            <div>
                 <h3>POD Info</h3>
                  <table border="1">
                    @foreach (var recordrow in db.Query(dbCommandPods)){
                     <tr>
                        <td> @recordrow.PodName</td>                          
                    </tr> 
                     }      
                    <tr>
                        <td>Practice Manager: @PractManagerFname @PractManagerLname</td>                         
                    </tr>
                    <tr>
                        <td>Practice Manager Email: @PractManagerEmail</td>
                    </tr>
                  </table>                          
            </div>                                            
    </body>
</html>

Open in new window

polaris101Asked:
Who is Participating?
 
Christopher KileConnect With a Mentor Commented:
The variables db and dbCommandPods are not in scope where you're using them.  They're only available in the scope where they're declared.  You could move the declarations up to where fname is declared, but then you would have to keep your initial assignments of values to these variables in the same place where you are checking if MemberId has been passed in the QueryString.  Assuming this, you would later have to check to see if db got initialized before you attempted to run your @foreach loop (if db got initialized, then dbCommandPods was initialized as well).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.