//We also need to show the the page content to be able to update the changes we make. protected void ShowPageContent(PagesDB db) { bool valid = true; string pageid = Request.QueryString["pageid"]; if (String.IsNullOrEmpty(pageid)) { valid = false; } if (valid) { HTTP_Page page_record = db.FindPage(Int32.Parse(pageid)); page_title.Text = page_record.GetPagetitle(); page_body.Text = page_record.GetPagebody(); page_author.Text = page_record.GetPageauthor(); page_date.InnerHtml = page_record.GetTimeStamp().ToString("yyyy-MM-dd"); } else { valid = false; } if (!valid) { page_content.InnerHtml = "There was an error finding that Page."; } }
// We create the edit page so that we can edit data, through the Edit Page method we can set the new values the user updates. protected void Edit_Page(object sender, EventArgs e) { PagesDB db = new PagesDB(); bool valid = true; string pageid = Request.QueryString["pageid"]; if (String.IsNullOrEmpty(pageid)) { valid = false; } if (valid) { HTTP_Page new_page = new HTTP_Page(); new_page.SetPagetitle(page_title.Text); new_page.SetPagebody(page_body.Text); new_page.SetPageauthor(page_author.Text); try { db.EditPage(Int32.Parse(pageid), new_page); Response.Redirect("ShowPage.aspx?pageid=" + pageid); } catch { valid = false; } } if (!valid) { page_content.InnerHtml = "There was an error updating that page."; } }
//We haave to set up a method so that when the user clicks submit, the information goes into the database and into the "Manage Pages" page. //We set up connection to the PagesDB and SET the information into the values. protected void Add_Page(object sender, EventArgs e) { PagesDB db = new PagesDB(); HTTP_Page new_page = new HTTP_Page(); new_page.SetPagetitle(newpage_title.Text); new_page.SetPagebody(newpage_body.Text); new_page.SetPageauthor(newpage_author.Text); new_page.SetTimeStamp(DateTime.Now); db.AddPage(new_page); Response.Redirect("ManagePages.aspx"); }
// we create a method so that we can add a page which directly sends the information to the table in the database. public void AddPage(HTTP_Page new_page) { //We write the query to instruct the method what we want it to do string query = "insert into pagesmgmt (pagetitle, pagebody, pageauthor, timestamp) values ('{0}','{1}','{2}','{3}')"; query = String.Format(query, new_page.GetPagetitle(), new_page.GetPagebody(), new_page.GetPageauthor(), new_page.GetTimeStamp().ToString("yyyy-MM-dd")); MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); try { Connect.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { //We create debugs so that if something goes wrong we know on what part it was Debug.WriteLine("Something went wrong in the AddPage Method!"); Debug.WriteLine(ex.ToString()); } Connect.Close(); }
public void EditPage(int pageid, HTTP_Page new_page) { string query = "update pagesmgmt set pagetitle='{0}', pagebody='{1}', pageauthor='{2}' where pageid={3}"; query = String.Format(query, new_page.GetPagetitle(), new_page.GetPagebody(), new_page.GetPageauthor(), pageid); MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); try { Connect.Open(); cmd.ExecuteNonQuery(); Debug.WriteLine("Executed query " + query); } catch (Exception ex) { //We create debugs so that if something goes wrong we know on what part it was Debug.WriteLine("Something went wrong in the EditPage method"); Debug.WriteLine(ex.ToString()); } Connect.Close(); }
// We create another method for when a user wants to view a page that is already in the database. public HTTP_Page FindPage(int id) { MySqlConnection Connect = new MySqlConnection(ConnectionString); HTTP_Page result_page = new HTTP_Page(); try { //Again we write a query specific to finding pages depending on the id number by connecting it to the database string query = "select * from pagesmgmt where pageid =" + id; Debug.WriteLine("Connection Initialized..."); Connect.Open(); MySqlCommand cmd = new MySqlCommand(query, Connect); MySqlDataReader resultset = cmd.ExecuteReader(); List <HTTP_Page> pages = new List <HTTP_Page>(); while (resultset.Read()) { HTTP_Page currentpage = new HTTP_Page(); for (int i = 0; i < resultset.FieldCount; i++) { string key = resultset.GetName(i); string value = resultset.GetString(i); Debug.WriteLine("Attempting to transfer " + key + " data of " + value); // We make sure to write the correct variables so that the information matches switch (key) { case "pagetitle": currentpage.SetPagetitle(value); break; case "pagebody": currentpage.SetPagebody(value); break; case "pageauthor": currentpage.SetPageauthor(value); break; case "timestamp": currentpage.SetTimeStamp(DateTime.Parse(value)); //we have to get the time that the databas automatically created break; } } pages.Add(currentpage); } result_page = pages[0]; } catch (Exception ex) { //We create debugs so that if something goes wrong we know on what part it was Debug.WriteLine("Something went wrong in the find Page method!"); Debug.WriteLine(ex.ToString()); } Connect.Close(); Debug.WriteLine("Database Connection Terminated."); return(result_page); }