I have a simple form that inputs an article into my database. It is called Create-Article.aspx. It contains 4 textboxs and a button. Upon submitting the form via the button, it calls some code from my DAL that inserts this new article into my DB. This all works fine. I added a feature whereby an article can be edited. To edit an article, a user clicks an 'edit this article' link from the page that displays all the articles. They will then be sent to Create-Article.aspx?edit=articleID (articleID is the identity column of my articles table). The issue I have is that when I eventually submit this finalized 'edit' of the article it never actually changes the article. The codebehind of Create-Article.aspx is as follows: protected void Page_Load(object sender, EventArgs e) { Protection.RestrictTo(Protection.UserGroup.Administrator); DAL = new ArticleAccess(); if (Request.QueryString["edit"] != null) { articleId = Convert.ToInt32(Request.QueryString["edit"]); lblMode.Text = "Editing an existing article"; DataTable tempArticle = DAL.getArticle(articleId); txtSeoFilename.Text = (string)tempArticle.Rows[0]["vc_seo"]; txtArticle.Text = (string)tempArticle.Rows[0]["vc_text"]; txtSeoCategory.Text = (string)tempArticle.Rows[0]["vc_category"]; txtTitle.Text = (string)tempArticle.Rows[0]["vc_title"]; } } Code (markup): Basically summarizing the sloppy code above... If the page comes through with "?edit" in the URL, then get the edit querystring and convert it to an Int32. then query SQL, and get the relevant article then fillout the textboxes with the contents of the article If the page comes through WITHOUT "?edit" in the url then nothing happens (in which case the 4 inputs appear as they are arranged in the aspx, blank) This works fine, I can load up the page and it will fill out the textboxs with the article.. I can edit it and type whatever I want. However when i push submit nothing changes. Here' is the code that runs when i push submit. protected void Button1_Click(object sender, EventArgs e) { if (Request.QueryString["edit"] == null) { this.createNewArticle(); //Response.Write("?edit was null");//for diagnostic purposes } else { this.editExistingArticle(articleId); //Response.Write("?edit was NOT null");//for diagnostic purposes } Response.Redirect("~/Admin/Article-Manager/"); //this is a paginated view of articles } private void createNewArticle() { DAL = new ArticleAccess(); DAL.createArticle( txtSeoFilename.Text, txtArticle.Text, txtSeoCategory.Text, txtTitle.Text); } private void editExistingArticle(int articleId) { DAL = new ArticleAccess(); DAL.editArticle( articleId, txtSeoFilename.Text, txtArticle.Text, txtSeoCategory.Text, txtTitle.Text); } Code (markup): Summarizing this code... If this page came through without "?edit" in the Url... then we are making a new article! use the createArticle() code //in my DAL this is an insert statement else if this page came through with an "?edit" in the url then we are editing an existing article! use the editArticle() code //in my DAL this is an update statement I don't know why my articles can be created perfectly fine, but when edited they stay the same. Does it have something to do with setting the values of the textbox at page load time? Perhaps these textbox values never actually change in the way I expect.. I'm including my DAL code just to be thorough but I must note that it has been tested and works. public bool createArticle(string seoFileName, string articleText, string articleCategory, string articleTitle) { bool ret = false; using (SqlConnection sqlConn = new SqlConnection(connectionString)) { SqlCommand insertCmd = sqlConn.CreateCommand(); insertCmd.Parameters.AddWithValue("@seoFileName", seoFileName); insertCmd.Parameters.AddWithValue("@articleText", articleText); insertCmd.Parameters.AddWithValue("@articleCategory", articleCategory); insertCmd.Parameters.AddWithValue("@articleTitle", articleTitle); insertCmd.CommandText = "INSERT INTO LWG_articles(vc_seo,vc_text,vc_category,vc_title)" + "VALUES(@seoFileName,@articleText,@articleCategory,@articleTitle)"; sqlConn.Open(); insertCmd.ExecuteNonQuery(); ret = true; } return ret; } public bool editArticle(int articleId, string seoFileName, string articleText, string articleCategory, string articleTitle) { bool ret = false; using (SqlConnection sqlConn = new SqlConnection(connectionString)) { SqlCommand updateCmd = sqlConn.CreateCommand(); updateCmd.Parameters.AddWithValue("@articleId", articleId); updateCmd.Parameters.AddWithValue("@seoFileName", seoFileName); updateCmd.Parameters.AddWithValue("@articleText", articleText); updateCmd.Parameters.AddWithValue("@articleCategory", articleCategory); updateCmd.Parameters.AddWithValue("@articleTitle", articleTitle); updateCmd.CommandText = "UPDATE LWG_articles " + "SET vc_seo = @seoFileName, " + "vc_text = @articleText, " + "vc_category = @articleCategory, " + "vc_title = @articleTitle " + "WHERE id = @articleId"; sqlConn.Open(); updateCmd.ExecuteNonQuery(); ret = true; } return ret; } Code (markup):
Try protected void Page_Load(object sender, EventArgs e) { Protection.RestrictTo(Protection.UserGroup.Administrator); if(!IsPostBack) { DAL = new ArticleAccess(); if (Request.QueryString["edit"] != null) { articleId = Convert.ToInt32(Request.QueryString["edit"]); lblMode.Text = "Editing an existing article"; DataTable tempArticle = DAL.getArticle(articleId); txtSeoFilename.Text = (string)tempArticle.Rows[0]["vc_seo"]; txtArticle.Text = (string)tempArticle.Rows[0]["vc_text"]; txtSeoCategory.Text = (string)tempArticle.Rows[0]["vc_category"]; txtTitle.Text = (string)tempArticle.Rows[0]["vc_title"]; } } } Code (markup):
What happens is that when you click the submit button in order to edit your data, the button causes a post back. On a post back, the PageLoad is executed again. So, when you edit information in the text boxes, they are reloaded from your DAL upon post back (hence wiping out your changes). Then your data is actually is being updated to the datasource, it just happens to be the exact same data.
Awesome sir! Thank you thats exactly what was happening. functional code: protected void Page_Load(object sender, EventArgs e) { Protection.RestrictTo(Protection.UserGroup.Administrator); DAL = new ArticleAccess(); if (Request.QueryString["edit"] != null) { articleId = Convert.ToInt32(Request.QueryString["edit"]); lblMode.Text = "Editing an existing article"; if (!IsPostBack) { DataTable tempArticle = DAL.getArticle(articleId); txtSeoFilename.Text = (string)tempArticle.Rows[0]["vc_seo"]; txtArticle.Text = (string)tempArticle.Rows[0]["vc_text"]; txtSeoCategory.Text = (string)tempArticle.Rows[0]["vc_category"]; txtTitle.Text = (string)tempArticle.Rows[0]["vc_title"]; } } } Code (markup):