• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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

0
polaris101
Asked:
polaris101
1 Solution
 
Christopher KileCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now