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());
                }
            }
        }
Ejemplo n.º 2
0
        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");
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        //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);
        }