//if the user clicks on the Update Page button protected void UpdatePage(object sender, EventArgs e) { //Create our database object HTTP_PAGEDB db = new HTTP_PAGEDB(); //Our flag to know if the data can be obtained or not bool valid = true; //Get the http_pageID from the query string http_pageID = Request.QueryString["http_pageID"]; //if there is no id then user must have went to this page without a id if (String.IsNullOrEmpty(http_pageID)) { valid = false; } //if there is a id on the page then if (valid) { //Create a new HTTP_Page object HTTP_Page newPage = new HTTP_Page(); //Set the necessary value in the HTTP_Page object that will get updated newPage.SetTitle(pageTitle.Text); newPage.SetContent(pageContent.Text); try { //Update the HTTP_Page to the database db.UpdatePage(Int32.Parse(http_pageID), newPage); //Go back to the show page of this particular HTTP_Page we are updating Response.Redirect("ShowHTTP_Page.aspx?http_pageID=" + http_pageID); } catch { valid = false; } } //No id was found or failed to update if (!valid) { http_page.InnerHtml = "There was an error updating that page."; } }
//When user clicks on the Add Page button protected void AddPage(object sender, EventArgs e) { //Create connection HTTP_PAGEDB db = new HTTP_PAGEDB(); //Create a new particular HTTP_Page HTTP_Page newPage = new HTTP_Page(); //Set that page data newPage.SetTitle(pageTitle.Text); newPage.SetContent(pageContent.Text); newPage.SetDate(DateTime.Today); //Add the page to the database db.AddPage(newPage); //Go back to the list of HTTP_Pages Response.Redirect("Manage_HTTP_Pages.aspx"); }
//This code is referenced from SCHOOLDB.cs in our examples public List <HTTP_Page> List_Query(string query) { //Set up SQL Connection MySqlConnection Connect = new MySqlConnection(ConnectionString); //Our list of HTTP_Page that is the result from the query List <HTTP_Page> ResultSet = new List <HTTP_Page>(); try { Debug.WriteLine("Connection Initialized..."); Debug.WriteLine("Attempting to execute query " + query); //open the db connection Connect.Open(); //give the connection a query MySqlCommand cmd = new MySqlCommand(query, Connect); //grab the result set MySqlDataReader resultset = cmd.ExecuteReader(); //for every row in the result set while (resultset.Read()) { //Create a new HTTP_Page object HTTP_Page Row = new HTTP_Page(); //for every column in the row for (int i = 0; i < resultset.FieldCount; i++) { //Switch statement to set the value to their correct column switch (resultset.GetName(i)) { case "HTTPPAGEID": Row.SetID(resultset.GetString(i)); break; case "HTTPPAGETITLE": Row.SetTitle(resultset.GetString(i)); break; case "HTTPPAGECONTENT": Row.SetContent(resultset.GetString(i)); break; case "HTTPPAGEDATE": Row.SetDate(DateTime.Parse(resultset.GetString(i))); break; default: Debug.WriteLine("The column name is not in the HTTP_Page"); break; } } //Add the object to the list result ResultSet.Add(Row); }//end row //Finish reading (no more content left) resultset.Close(); } //Exception occured catch (Exception ex) { Debug.WriteLine("Something went wrong in the List_Query method!"); Debug.WriteLine(ex.ToString()); } //Close the connection to the database Connect.Close(); Debug.WriteLine("Database Connection Terminated."); //Return our result return(ResultSet); }
//This code is referenced from SCHOOLDB.cs in our examples public HTTP_Page FindPage(int id) { //Utilize the connection string MySqlConnection Connect = new MySqlConnection(ConnectionString); //Create a new HTTP_Page Object to be used for are return value HTTP_Page result_page = new HTTP_Page(); //we will try to grab HTTP_Page data from the database, if we fail, a message will appear in Debug>Windows>Output dialogue try { //Build a custom query with the id information provided string query = "select * from http_pages where HTTPPAGEID = " + id; Debug.WriteLine("Connection Initialized..."); //open the db connection Connect.Open(); //Run out query against the database MySqlCommand cmd = new MySqlCommand(query, Connect); //grab the result set MySqlDataReader resultset = cmd.ExecuteReader(); //Create a list of pages (although we're only trying to get 1) List <HTTP_Page> pages = new List <HTTP_Page>(); //read through the result set while (resultset.Read()) { //information that will store a single page HTTP_Page currentPage = new HTTP_Page(); //Look at each column in the result set row, add both the column name and the column value to our Student dictionary 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 statement to set the value to their correct column switch (key) { case "HTTPPAGEID": currentPage.SetID(value); break; case "HTTPPAGETITLE": currentPage.SetTitle(value); break; case "HTTPPAGECONTENT": currentPage.SetContent(value); break; case "HTTPPAGEDATE": currentPage.SetDate(DateTime.Parse(value)); break; default: Debug.WriteLine("The column name is not in the HTTP_Page"); break; } } //Add the page to the list of pages pages.Add(currentPage); } //get the first page result_page = pages[0]; } //An exception occured catch (Exception ex) { //If something (anything) goes wrong with the try{} block, this block will execute Debug.WriteLine("Something went wrong in the find Page method!"); Debug.WriteLine(ex.ToString()); } //Close the database connection Connect.Close(); Debug.WriteLine("Database Connection Terminated."); //Return our result return(result_page); }