Looping Through Database in ASP/C# to Pull All Data from Junction Table
<p>This is related to my last post about pulling data from a junction table. I figured out how to get the data but it's not pulling all the instances. Here's the setup:</p>

<p>I have a MS Access Database that has 3 tables: Students, Courses, StudentToCourse. StudentToCourse represents what course is associated to what student where a student can be registered to many courses. </p>

<p>My code only pulls up one instances meaning that it displays only 1 course that the student is tied to even though the student may have more instances in the StudentToCourse table but with different courses.</p>

<p>I know I need to loop and concatenate the strings so it displays the rest of the info but i'm not sure what I'm using as the conditions of the loop nor where to concatenate. </p>

<pre><code>&lt;%@Page Language="C#"%&gt;
&lt;%@ Import Namespace="System.Data"%&gt;
&lt;%@ Import Namespace="System.Data.Common"%&gt;
&lt;%@ Import Namespace="System.Data.OleDb"%&gt;
&lt;%@ Import Namespace="System.Configuration"%&gt;
&lt;%@ Import Namespace="System.Collections.Generic"%&gt;

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;ADO Basic Example&lt;/title&gt;
    &lt;link type="text/css" rel="stylesheet" href="../style/style.css" /&gt;
    &lt;script runat="server" language="C#"&gt;

            void Page_Load()
            {
                if(Session["LoggedInId"]==null)
                {
                    Response.Redirect("Login.aspx?error=1");
                }

                lblUserFirstName.Text = Session["FirstName"].ToString();
                Load();

            }

            void LogOut(object sender, EventArgs e)
            {
                Session["LoggedInId"]=null;
                Session["FirstName"]=null;
                Session["LastName"]=null;
                Response.Redirect("login.aspx");
            }



            void Load()
            {
                String provider = ConfigurationManager.ConnectionStrings["studentConnString"].ProviderName;

                DbProviderFactory factory = DbProviderFactories.GetFactory(provider);

                //Open a Connection
                DbConnection conn = factory.CreateConnection();

                //Assign a Connection String
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["studentConnString"].ConnectionString;

                //Connection Open
                conn.Open();

                //Initialize a Command
                DbCommand comm = conn.CreateCommand();

                //Tell the command which connection it will use
                comm.Connection = conn;

                //Give the command SQL to execute   
                comm.CommandText = "SELECT Courses.Id, Courses.CourseSubject, Courses.CourseName, Courses.CourseNumber, Courses.CourseDescription FROM Courses, StudentToCourse, Students WHERE Courses.Id = StudentToCourse.Courseid AND StudentToCourse.Studentid = ?";

                DbParameter param;
                param = comm.CreateParameter();
                param.DbType = DbType.Int32;
                param.Direction = ParameterDirection.Input;
                param.Value = Session["LoggedInId"];

                comm.Parameters.Add(param);

                //Execute the command and get back the results via a reader
                DbDataReader reader = comm.ExecuteReader();

                //While we get results from the DB, add a row to the Table
                while (reader.Read())
                {
                    lblCourseSubject.Text = reader["CourseSubject"].ToString();
                    lblCourseNumber.Text = reader["CourseNumber"].ToString();
                    lblCourseName.Text = reader["CourseName"].ToString();
                    lblCourseDesc.Text = reader["CourseDescription"].ToString();
                }

                //Free up the connection
                conn.Close();

            }


    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form runat="server"&gt;
    &lt;div id="mainBody"&gt;
        &lt;header&gt;
            &lt;hgroup&gt;
                &lt;h1&gt;&lt;asp:Label ID="lblUserFirstName" runat="server"&gt;&lt;/asp:Label&gt;'s Enrolled Courses&lt;/h1&gt;
            &lt;/hgroup&gt;
        &lt;/header&gt;
        &lt;section&gt;

            &lt;p&gt;Course Enrollment Summary&lt;/p&gt;

            &lt;div class="courseInfo"&gt;
                &lt;div class="courseId"&gt;&lt;span&gt;&lt;asp:Label ID="lblCourseSubject" runat="server"&gt;&lt;/asp:Label&gt;&lt;/span&gt;&lt;span&gt;&lt;asp:Label ID="lblCourseNumber" runat="server"&gt;&lt;/asp:Label&gt;&lt;/span&gt;&lt;/div&gt;
                &lt;div class="courseName"&gt;&lt;asp:Label runat="server" ID="lblCourseName"&gt;&lt;/asp:Label&gt;&lt;/div&gt;
                &lt;div class="courseDesc"&gt;&lt;asp:Label runat="server" ID="lblCourseDesc"&gt;&lt;/asp:Label&gt;&lt;/div&gt;
            &lt;/div&gt;

            &lt;/br&gt;
            &lt;asp:Button ID="btnLogout" runat="server" Text="LogOut" OnClick="LogOut"&gt;&lt;/asp:Button&gt;

        &lt;/section&gt;
        &lt;footer&gt;
        &lt;/footer&gt;
    &lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
