Example #1
0
        protected void Add_Page(object sender, EventArgs e)
        {
            //create connection
            Dbconnection db = new Dbconnection();

            //create a new particular student
            HttpPage new_HttpPage = new HttpPage();

            //set that student data
            new_HttpPage.SetPageTitle(pagetitle.Text);
            new_HttpPage.SetPageContent(pagecontent.Text);
            new_HttpPage.SetPageAuthor(pageauthor.Text);

            DateTime dateTimeVariable = DateTime.Now;
            string   date             = dateTimeVariable.ToString("yyyy-MM-dd H:mm:ss");


            new_HttpPage.SetPagePublish_Date(date);

            //add the student to the database
            db.AddPage(new_HttpPage);


            Response.Redirect("ListPages.aspx");
        }
        protected void Update_Page(object sender, EventArgs e)
        {
            //this connection instance is for editing data
            Dbconnection db = new Dbconnection();

            bool   valid  = true;
            string Pageid = Request.QueryString["pageid"];

            if (!String.IsNullOrEmpty(Pageid))
            {
                HttpPage new_HttpPage = new HttpPage();
                //set that Page data
                new_HttpPage.SetPageTitle(pagetitle.Text);
                new_HttpPage.SetPageContent(pagecontent.Text);
                new_HttpPage.SetPageAuthor(pageauthor.Text);

                //add the Page data to the database
                try
                {
                    db.UpdatePage(Int32.Parse(Pageid), new_HttpPage);
                    Response.Redirect("ViewPage.aspx?pageid=" + Pageid);
                }
                catch
                {
                    valid = false;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //this connection instance is for showing data

                Dbconnection db     = new Dbconnection();
                bool         valid  = true;
                string       pageid = Request.QueryString["pageid"];
                Debug.WriteLine("PageID:" + pageid);
                if (String.IsNullOrEmpty(pageid))
                {
                    valid = false;
                }

                //We will attempt to get the record we need
                if (valid)
                {
                    HttpPage page_record = db.FindHttpPage(Int32.Parse(pageid));

                    pagetitle.Text   = page_record.GetPageTitle();
                    pagecontent.Text = page_record.GetPageContent();
                    pageauthor.Text  = page_record.GetPageAuthor();
                }
            }
        }
        //Get All Pages From Databas
        public HttpPage FindHttpPage(int id)
        {
            MySqlConnection Connect         = new MySqlConnection(ConnectionString);
            HttpPage        result_HttpPage = new HttpPage();

            try
            {
                string query = "select * from pages where pageid = " + id;
                Debug.WriteLine("Connection Initialized...");
                Connect.Open();
                MySqlCommand    cmd       = new MySqlCommand(query, Connect);
                MySqlDataReader resultset = cmd.ExecuteReader();
                List <HttpPage> Pages     = new List <HttpPage>();
                while (resultset.Read())
                {
                    HttpPage currentHttpPage = new HttpPage();
                    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 "pagetitle":
                            currentHttpPage.SetPageTitle(value);
                            break;

                        case "pagebody":
                            currentHttpPage.SetPageContent(value);
                            break;

                        case "author":
                            currentHttpPage.SetPageAuthor(value);
                            break;

                        case "Publish_Date":
                            currentHttpPage.SetPagePublish_Date(value);
                            break;
                        }
                    }

                    Pages.Add(currentHttpPage);
                }

                result_HttpPage = Pages[0];
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Something went wrong in the find page method!");
                Debug.WriteLine(ex.ToString());
            }
            Connect.Close();
            Debug.WriteLine("Database Connection Terminated.");
            return(result_HttpPage);
        }
        // Update page
        public void UpdatePage(int pageid, HttpPage new_HttpPage)
        {
            string query = "update pages set pagetitle='{0}', pagebody='{1}', author='{2}' where pageid={3}";

            query = String.Format(query, new_HttpPage.GetPageTitle(), new_HttpPage.GetPageContent(), new_HttpPage.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)
            {
                Debug.WriteLine("Something went wrong in the Updatepage Method!");
                Debug.WriteLine(ex.ToString());
            }
            Connect.Close();
        }
        //Add  New Page to database
        public void AddPage(HttpPage new_HttpPage)
        {
            string query = "insert into pages (pagetitle, pagebody, author, Publish_Date) values ('{0}','{1}','{2}','{3}')";

            query = String.Format(query, new_HttpPage.GetPageTitle(), new_HttpPage.GetPageContent(), new_HttpPage.GetPageAuthor(), new_HttpPage.GetPagePublish_Date());
            MySqlConnection Connect = new MySqlConnection(ConnectionString);
            MySqlCommand    cmd     = new MySqlCommand(query, Connect);

            try
            {
                Connect.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Something went wrong in the Addpage Method!");
                Debug.WriteLine(ex.ToString());
            }

            Connect.Close();
        }
        public void ShowPageInfo(Dbconnection db)
        {
            bool   valid  = true;
            string pageid = Request.QueryString["pageid"];

            if (String.IsNullOrEmpty(pageid))
            {
                valid = false;
            }
            if (valid)
            {
                //finding  the page we requested by refering the function FindHttpPage in Dbconnection.cs and gets the records to display on html page
                HttpPage page_record = db.FindHttpPage(Int32.Parse(pageid));
                heading.InnerHtml     = page_record.GetPageTitle();
                pagecontent.InnerHtml = page_record.GetPageContent();
                author.InnerHtml      = "Author:" + " " + page_record.GetPageAuthor();
                Publishdate.InnerHtml = "Published on" + " " + page_record.GetPagePublish_Date();
            }
            else
            {
                valid = false;
            }
        }