Ejemplo n.º 1
0
        protected void AddButton_Click(object sender, EventArgs e)
        {
            WebsiteDB db = new WebsiteDB();

            Webpage webpage = new Webpage();

            //set the title, body and author from the textbox to the webpage class
            webpage.set_W_title(txtpage_title.Text);
            webpage.set_W_body(txtpage_body.InnerText);
            webpage.set_W_author(ddpage_author.SelectedValue);
            //if user checks the publish checkbox set the state to published and publish date to the current date and time
            if (chkpublish.Checked == true)
            {
                webpage.set_W_publish_state("Published");
                //convert date and time to ex.(Saturday, 15 July 2019 5:00 AM)
                webpage.set_W_publish_date(DateTime.Now.ToString("dddd, dd MMMM yyyy h:mm tt"));
            }
            //if user doesnt check the checkbox set the state to not published
            else
            {
                webpage.set_W_publish_state("Not Published");
            }
            //use the webpage class to pass the variables to the addwebpage method in the database class
            db.AddWebpage(webpage);

            Response.Redirect("ListWebpages.aspx");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            bool flag   = true;
            int  pageid = Convert.ToInt32(Request.QueryString["pageid"]);

            if (pageid.Equals(null))
            {
                flag = false;
            }

            if (flag == true)
            {
                var    db    = new WebsiteDB();
                string query = "select * from pages join author on publish_author = author_id where page_id = " + pageid;
                List <Dictionary <string, string> > page = db.List_Query(query);

                if (page.Count > 0)
                {
                    foreach (Dictionary <String, String> pagedata in page)
                    {
                        Webpage webpage      = new Webpage();
                        string  pagetitle    = pagedata["page_title"];
                        string  pagebody     = pagedata["page_body"];
                        string  pageauthor   = pagedata["author_name"];
                        string  publishstate = pagedata["publish_state"];
                        string  publishdate  = pagedata["publish_date"];

                        title.InnerHtml = pagetitle;
                        webpage.set_W_title(pagetitle);
                        date.InnerHtml = publishdate;
                        body.InnerHtml = pagebody;
                        webpage.set_W_body(pagebody);
                        author.InnerHtml = pageauthor;
                        webpage.set_W_author(pageauthor);
                        state.InnerHtml = publishstate;
                        webpage.set_W_publish_state(publishstate);
                    }
                }
            }
        }
        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            WebsiteDB dB      = new WebsiteDB();
            Webpage   webpage = new Webpage();

            Boolean flag = true;

            //get the pageid from the request and convert the string to int
            int pageid = Convert.ToInt32(Request.QueryString["pageid"]);

            //if the id that requested is null or is 0 set the flag to false
            if (pageid.Equals(null) || pageid.Equals(0))
            {
                flag = false;
            }

            //if the flag is true continue with the logic
            if (flag)
            {
                //save the changes to the webpage in the webpage class
                webpage.set_W_title(txtpage_title.Text);
                webpage.set_W_body(txtpage_body.InnerText);
                webpage.set_W_author(ddpage_author.SelectedValue);

                //pass the new changes to the updatewebpages method in the websitedb class
                dB.UpdateWebpage(pageid, webpage);

                //once updated show the newly updated page
                Response.Redirect("SinglePage.aspx?pageid=" + pageid);
            }
            //if false show the alert of the error
            else
            {
                alert.Visible = true;
                alert.Attributes.Add("class", "alert alert-danger");
                output.InnerHtml = "Error cannot add";
            }
        }
Ejemplo n.º 4
0
        public Webpage FindWebPage(int id)
        {
            MySqlConnection sqlCon      = new MySqlConnection(ConnectionString);
            Webpage         result_page = new Webpage();

            try
            {
                string query = "select * from PAGES where page_id = " + id;

                sqlCon.Open();
                MySqlCommand    cmd       = new MySqlCommand(query, sqlCon);
                MySqlDataReader resultset = cmd.ExecuteReader();

                List <Webpage> page = new List <Webpage>();

                while (resultset.Read())
                {
                    Webpage currentpage = new Webpage();
                    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);
                        switch (key)
                        {
                        case "page_title":
                            currentpage.set_W_title(value);
                            break;

                        case "page_body":
                            currentpage.set_W_body(value);
                            break;

                        case "publish_author":
                            currentpage.set_W_author(value);
                            break;

                        case "publish_state":
                            currentpage.set_W_publish_state(value);
                            break;

                        case "publish_date":
                            currentpage.set_W_publish_date(value);
                            break;
                        }
                    }
                    page.Add(currentpage);
                }

                result_page = page[0];
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Check the findWebpage()");
                Debug.WriteLine(ex.ToString());
            }

            sqlCon.Close();
            Debug.WriteLine("Database Connection Terminated.");

            return(result_page);
        }