protected void Page_Load(object sender, EventArgs e) { // Creating an object of the CMSDB class to call the 'ViewPage(int pageid)' functions in the database file. CMSDB db = new CMSDB(); //Getting the pageid passed in the URL to select the current page from the database string pageid = Request.QueryString["pageid"]; //If pageid is empty the custom error message will be written, otherwise the page details will be fetched from the database. if (String.IsNullOrEmpty(pageid)) { pageId.InnerHtml = "Error findinf the page."; } else { //Creating an object of HTTP_Page to hold the HTTP_Page object which will be returned by the db.ViewPage(pageId) funciton. HTTP_Page page_record = db.ViewPage(Int32.Parse(pageid)); //Using getters to get the values from the page_record and putting them in their respective <div> tags. page_title.InnerHtml = page_record.GetPageTitle(); page_body.InnerHtml = page_record.GetPageContent(); //Creating a DateTime object which will hold the PUBLISHDATE received from the database. DateTime date = Convert.ToDateTime(page_record.GetPagePublishDate()); //Converting the date into the 'DD/MMM/YYYY/ format and writing them in the required div tags. page_publish_date.InnerHtml += date.ToString("dd/MMM/yyyy"); } }
protected void Update_Page(object sender, EventArgs e) { //This section of the code will get the updated details from the form and call the UpdatePage method. // Creating a CMSDB object to use the AddPage(HTTP_Page page) method. CMSDB db = new CMSDB(); //Getting the pageid from the Request object. string pageid = Request.QueryString["pageid"]; //If pageid is empty the custom error message will be written, otherwise the updated data in the form will be inserted into the database. if (String.IsNullOrEmpty(pageid)) { page_update.InnerHtml = "There was an error updating that student."; } else { //Creating a new HTTP_Page object which store the values retrieved from the form. HTTP_Page new_page = new HTTP_Page(); //Code to get the values from the form and set it to the 'new_page' object. new_page.SetPageTitle(page_title.Text); new_page.SetPageContent(page_content.Text); //if page_publish_box is checked true will be stored in pagePublishStatus which is of Boolean type if (page_publish_box.Checked) { new_page.SetPagePublishStatus(true); } else { new_page.SetPagePublishStatus(false); } // If 'Add new author' option in selected in the drop down, the data entered in the 'page_author' textbox will be inserted into the db. if (page_author_list.SelectedItem.Value == "New") { new_page.SetPageAuthor(page_author.Text); } else { new_page.SetPageAuthor(page_author_list.SelectedItem.Value); } //Calling the UpdatePage function to update the page details into the database using try catch blocks. try { db.UpdatePage(Int32.Parse(pageid), new_page); //After update the LispPages.aspx will be loaded. Response.Redirect("ListPages.aspx"); } catch (Exception ex) { Debug.WriteLine("Something went wrong in the UpdateStudent Method!"); Debug.WriteLine(ex.ToString()); } } }
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //If the page is not being loaded in response to a postBack the ShowPage() CMSDB db = new CMSDB(); // The below section of code is used to get the List of Authors from the Page table and display them in a dropdown box. Similar to CreatePage.aspx.cs List <HTTP_Page> pageList = new List <HTTP_Page>(); pageList = db.ListPages("select distinct pageauthor from page;"); foreach (HTTP_Page page in pageList) { ListItem item = new ListItem(); item.Text = page.GetPageAuthor(); item.Value = page.GetPageAuthor(); page_author_list.Items.Add(item); } ListItem newAuthorItem = new ListItem(); newAuthorItem.Text = "-------------- Add a new Author --------------"; newAuthorItem.Value = "New"; page_author_list.Items.Add(newAuthorItem); //Getting the pageid from the Request object string pageid = Request.QueryString["pageid"]; //If pageid is empty the custom error message will be written, otherwise the page details will be fetched from the database and shown in the input fields. if (String.IsNullOrEmpty(pageid)) { page_update.InnerHtml = "There was an error finding that student."; } else { HTTP_Page page_record = db.ViewPage(Int32.Parse(pageid)); page_update.InnerHtml = page_record.GetPageTitle(); page_title.Text = page_record.GetPageTitle(); page_content.Text = page_record.GetPageContent(); page_author_list.SelectedValue = page_record.GetPageAuthor(); page_publish_date.Text = page_record.GetPagePublishDate(); } } }
public void UpdatePage(int pageid, HTTP_Page new_page) { //SQL query to update the PAGE table with the new values. string query = "update PAGE set PAGETITLE='{0}', PAGEBODY='{1}', PAGEAUTHOR='{2}', PAGESTATUS='{3}' where PAGEID = '{4}'"; //To update values for PAGESTATUS field which is of type boolean. 0 is inserted if PAGESTATUS is false, 1 if true. int page_publish_value = 0; if (new_page.GetPagePublishStatus()) { page_publish_value = 1; } //Getting data from 'new_page' object which is passed in the parameter of the function during the function call. ('db.AddPage(new_page)') query = String.Format(query, new_page.GetPageTitle(), new_page.GetPageContent(), new_page.GetPageAuthor(), page_publish_value, pageid); //Creating a new connection object using the connection string and Creating an object for a MySqlCommand using query and the connect object. MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); try { //Opening the connection and executing the query on the database. Connect.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Debug.WriteLine("Exception in the updatePage(int pageId, HTTP_Page page) method!"); Debug.WriteLine(ex.ToString()); } finally { //Database connection is closed. Connect.Close(); } }
protected void Add_Page(object sender, EventArgs e) { // Creating a CMSDB object to use the AddPage(HTTP_Page page) method. CMSDB db = new CMSDB(); //Creating a new HTTP_Page object which store the values retrieved from the form. s HTTP_Page new_page = new HTTP_Page(); //Code to get the values from the form and set it to the 'new_page' object. new_page.SetPageTitle(page_title.Text); //if page_publish_box is checked true will be stored in pagePublishStatus which is of Boolean type new_page.SetPageContent(page_content.Text); if (page_publish_box.Checked) { new_page.SetPagePublishStatus(true); } else { new_page.SetPagePublishStatus(false); } // If 'Add new author' option in selected in the drop down, the data entered in the 'page_author' textbox will be inserted into the db. if (page_author_list.SelectedItem.Value == "New") { new_page.SetPageAuthor(page_author.Text); } else { new_page.SetPageAuthor(page_author_list.SelectedItem.Value); } //Calling the AddPage function to insert the page details into the database. db.AddPage(new_page); //Going back to ListPages.aspx after inserting the data. Response.Redirect("ListPages.aspx"); }
public void AddPage(HTTP_Page new_page) { //SQL query to insert data into the PAGE table string query = "insert into PAGE (PAGETITLE,PAGEBODY,PAGEDATE,PAGESTATUS,PAGEAUTHOR) values ('{0}','{1}',now(),'{2}','{3}')"; //To insert values for PAGESTATUS field which is of type boolean. 0 is inserted if PAGESTATUS is false, 1 if true. int page_publish_value = 0; if (new_page.GetPagePublishStatus()) { page_publish_value = 1; } //Getting data from 'new_page' object which is passed in the parameter of the function during the function call. ('db.AddPage(new_page)') query = String.Format(query, new_page.GetPageTitle(), new_page.GetPageContent(), page_publish_value, new_page.GetPageAuthor()); //Creating a new connection object using the connection string and Creating an object for a MySqlCommand using query and the connect object. MySqlConnection Connect = new MySqlConnection(ConnectionString); MySqlCommand cmd = new MySqlCommand(query, Connect); //try catch and finally for exception handling try { Connect.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Debug.WriteLine("Exception in the AddPage(HTTP_Page new_page) method"); Debug.WriteLine(ex.ToString()); } finally { Connect.Close(); } }
public HTTP_Page ViewPage(int id) { //Creating an object of class connect using the connectionString MySqlConnection Connect = new MySqlConnection(ConnectionString); //Creating an object of HTTP_Page which will be returned by the function HTTP_Page page = new HTTP_Page(); //Try block to catch exceptions that may arise from the code. try { //Below query will select the record with the pageid passed in the parameter. string query = "select * from PAGE where pageid = " + id; Debug.WriteLine("Connection Initialized..."); //Below lines open the connection, run the query against the db and get the resultset Connect.Open(); MySqlCommand cmd = new MySqlCommand(query, Connect); MySqlDataReader resultset = cmd.ExecuteReader(); //read through the result set while (resultset.Read()) { //To loop across each column in the PAGE table for (int i = 0; i < resultset.FieldCount; i++) { string key = resultset.GetName(i); string value = resultset.GetString(i); if (key == "pagetitle") { page.SetPageTitle(value); } if (key == "pagebody") { page.SetPageContent(value); } if (key == "pageauthor") { page.SetPageAuthor(value); } if (key == "pagedate") { page.SetPagePublishDate(value); } } } } catch (Exception ex) { //Writing the error in order to debug Debug.WriteLine("Exception in ViewPage(int id) method!"); Debug.WriteLine(ex.ToString()); } //Below block will execute even when there's an exception. finally { Connect.Close(); Debug.WriteLine("Database connection closed."); } //Returning the required page return(page); }
//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); }