//ListPages function returns a 'List of HTTP_Page objects'. Using these page objects the records are dynamically displayed. public List <HTTP_Page> ListPages(string query) { MySqlConnection Connect = new MySqlConnection(ConnectionString); // Creating a List of HTTP_Page objects, which will be modified and returned by the function. List <HTTP_Page> pageList = new List <HTTP_Page>(); try { //The below lines opens a connection, passes query to the connection and gets the result set after executing the query. Connect.Open(); MySqlCommand cmd = new MySqlCommand(query, Connect); MySqlDataReader resultset = cmd.ExecuteReader(); //Looping through the resultset to get the details needed to display in listPages. while (resultset.Read()) { //Creating page object which will hold individual record details. This will be added to the list ('pageList'). HTTP_Page page = new HTTP_Page(); Debug.WriteLine("Reading from the result Set"); //Code to get individual record details. for (int i = 0; i < resultset.FieldCount; i++) { string key = resultset.GetName(i); string value = resultset.GetString(i); if (key == "pageid") { page.SetPageId(Int32.Parse(value)); } if (key == "pagetitle") { page.SetPageTitle(value); } if (key == "pagebody") { page.SetPageContent(value); } if (key == "pageauthor") { page.SetPageAuthor(value); } if (key == "pagedate") { page.SetPagePublishDate(value); } if (key == "pagestatus") { page.SetPagePublishStatus(bool.Parse(value)); } } //Adding an individual page record to the pageList. pageList.Add(page); } resultset.Close(); } catch (Exception ex) { Debug.WriteLine("An exception occured in the ListPages(String query) method! "); Debug.WriteLine(ex.ToString()); } finally { //Closing the connection Connect.Close(); } Debug.WriteLine("Database Connection Closed."); //Returning pageList which has a list of HTTP_Page objects. return(pageList); }