public BLOGPOST FindBlogPost(int id) { MySqlConnection Connect = new MySqlConnection(ConnectionString); BLOGPOST result_blogpost = new BLOGPOST(); try { string query = "select * from blog_post where blogid = " + id; Debug.WriteLine("connection initialized..."); Connect.Open(); MySqlCommand cmd = new MySqlCommand(query, Connect); MySqlDataReader resultset = cmd.ExecuteReader(); List <BLOGPOST> BlogPosts = new List <BLOGPOST>(); while (resultset.Read()) { BLOGPOST currentblogpost = new BLOGPOST(); 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 "blogtitle": currentblogpost.SetBPTitle(value); break; case "blogbody": currentblogpost.SetBPBody(value); break; } } BlogPosts.Add(currentblogpost); } result_blogpost = BlogPosts[0]; } catch (Exception ex) { Debug.WriteLine("Something went wrong in the find blog method!"); Debug.WriteLine(ex.ToString()); } Connect.Close(); Debug.WriteLine("Database Connection Terminated"); return(result_blogpost); }
protected void AddPost(object sender, EventArgs e) { BLOGDB db = new BLOGDB(); BLOGPOST new_post = new BLOGPOST(); new_post.SetBPTitle(blog_title.Text); new_post.SetBPBody(blog_body.Text); db.AddBlogPost(new_post); Response.Redirect("ListBlogPost.aspx"); }
protected void Update_Blog(object sender, EventArgs e) { BLOGDB db = new BLOGDB(); bool valid = true; string blogid = Request.QueryString["blogid"]; if (String.IsNullOrEmpty(blogid)) { valid = false; } Debug.WriteLine("i am trying to update the blog with id=" + blogid); if (valid) { BLOGPOST new_blog = new BLOGPOST(); new_blog.SetBPTitle(blog_title.Text); new_blog.SetBPBody(blog_post.Text); Debug.WriteLine("the new blog title is " + blog_title.Text + " and the new blog body is " + blog_post.Text); try { db.UpdateBlogPost(Int32.Parse(blogid), new_blog); Response.Redirect("ShowBlogPost.aspx?blogid=" + blogid); } catch { Debug.WriteLine("now im in the catch, is it working?"); valid = false; } } if (!valid) { Debug.WriteLine("last chance, but we kinda already know we got here"); blog.InnerHtml = "There was an error updating your blog"; } }