public List <HTMLPAGE> List_Query(string query) { List <HTMLPAGE> Pages = new List <HTMLPAGE>(); List <Dictionary <string, string> > ResultSet = new List <Dictionary <string, string> >(); MySqlConnection Connect = ConnectDatabase(); try { // connect to db Connect.Open(); MySqlCommand cmd = new MySqlCommand(query, Connect); // get result set ( class is mysqldatareader ) MySqlDataReader resultset = cmd.ExecuteReader(); // move result set from mysqldatareader to list<dictionary> while (resultset.Read()) { Dictionary <String, String> Row = new Dictionary <String, String>(); //for every column in the row for (int i = 0; i < resultset.FieldCount; i++) { Row.Add(resultset.GetName(i), resultset.GetString(i)); } ResultSet.Add(Row); } // no need to use resultset anymore resultset.Close(); } catch (Exception ex) { Debug.WriteLine("exception when connecting to db: "); Debug.WriteLine(ex.ToString()); } // close connection Connect.Close(); foreach (Dictionary <string, string> row in ResultSet) { string pageid = row["pageid"]; string pagetitle = row["pagetitle"]; string pagebody = row["pagebody"]; string isPublished = row["isPublished"]; // new instance of page HTMLPAGE page = new HTMLPAGE { PageId = pageid, PageTitle = pagetitle, PageBody = pagebody, IsPublished = isPublished }; // add data to list page Pages.Add(page); } return(Pages); }
protected void Page_Load(object sender, EventArgs e) { // if goes here directly, something goes wrong. string pageid = Request.QueryString["pageid"]; if (String.IsNullOrEmpty(pageid)) { PageTitle.InnerText = "ERROR"; PageBody.InnerText = "This page does not exist"; return; } // query with pageid string query = "SELECT * FROM pages WHERE pageid = " + pageid; List <HTMLPAGE> Pages = new WEBSITEDB().List_Query(query); HTMLPAGE page = Pages[0]; string pagetitle = page.PageTitle; string pagebody = page.PageBody; string isPublished = page.IsPublished; // get the page id first PageTitle.InnerText = pagetitle; PageBody.InnerText = pagebody; // add edit button PageAction.InnerHtml = "<a class=\"btn btn-info\" href=\"EditPage.aspx?pageid=" + pageid + "\">Edit</a>"; // add delete or recover button if (isPublished == "True" || isPublished == "1") { // if isPublished, hide recover button RecoverBtn.Style.Add("display", "none"); } else { // if not, hide delete button DeleteBtn.Style.Add("display", "none"); } }