Saturday, March 31, 2012

Text box reading old value

Hi i am trying to create a simple page that would allow users to edit there infromation that is stored on them,

It loads the information from the database fine and puts it into the textbox's but then on the "update" button it doesnt get the changed values

e.g

if the textbox was set on the page load to "bren" and then i changed it on the page to be "Hello" when i execute the onlclick function of the button it still reads as "bren"

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.Odbc;public partialclass user_userDetails : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {if (!HttpContext.Current.User.Identity.Name.Contains(ConfigurationManager.AppSettings["siteGSTPrefix"])) {string userGuid = HttpContext.Current.User.Identity.Name; OdbcConnection detailsCon = sqlconnect.Connect(); OdbcCommand detailsCom =new OdbcCommand("SELECT tbl_usr.Username,tbl_usr_info.first_name,tbl_usr_info.second_name,tbl_usr_info.address_line_1,tbl_usr_info.address_line_2,tbl_usr_info.address_line_3,tbl_usr_info.post_code,tbl_usr_info.home_phone,tbl_usr_info.work_phone,tbl_usr_info.email_addy FROM tbl_usr INNER JOIN tbl_usr_info ON tbl_usr.usr_guid = tbl_usr_info.usr_guid WHERE tbl_usr.usr_guid='" + userGuid +"'", detailsCon); OdbcDataReader detRead = detailsCom.ExecuteReader();while (detRead.Read()) { userNote.Text ="Your Password is not displayed for security!"; userName.Text = detRead["Username"].ToString().Replace("#@dotnet.itags.org.","'"); fName.Text = detRead["first_name"].ToString().Replace("#@dotnet.itags.org.","'"); sName.Text = detRead["second_name"].ToString().Replace("#@dotnet.itags.org.","'"); aD1.Text = detRead["address_line_1"].ToString().Replace("#@dotnet.itags.org.","'"); aD2.Text = detRead["address_line_2"].ToString().Replace("#@dotnet.itags.org.","'"); aD3.Text = detRead["address_line_3"].ToString().Replace("#@dotnet.itags.org.","'"); pCode.Text = detRead["post_code"].ToString().Replace("#@dotnet.itags.org.","'"); hPhone.Text = detRead["home_phone"].ToString().Replace("#@dotnet.itags.org.","'"); wPhone.Text = detRead["work_phone"].ToString().Replace("#@dotnet.itags.org.","'"); eAddy.Text = detRead["email_addy"].ToString().Replace("#@dotnet.itags.org.","'"); } detRead.Close(); detailsCon.Close(); }else { Response.Redirect(FormsAuthentication.LoginUrl); } }protected void update_click(object sender, EventArgs e) {string userGuid = HttpContext.Current.User.Identity.Name;string passSettings =string.Empty; Response.Clear();if(userPass.Text.ToString() !="" && userPass.Text.ToString() !=null) { passSettings =" , tbl_usr.Password = '" + userPass.Text.ToString().Replace("'","#@dotnet.itags.org.") +"'"; } OdbcConnection detCon = sqlconnect.Connect(); OdbcCommand upCom =new OdbcCommand("UPDATE tbl_usr SET tbl_usr.Username = '" + userName.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' " + passSettings +" WHERE tbl_usr.usr_guid = '" + userGuid +"'", detCon); upCom.ExecuteNonQuery(); OdbcCommand upComInfo =new OdbcCommand("UPDATE tbl_usr_info SET tbl_usr_info.first_name = '" + fName.Text.Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.second_name = '" + sName.Text.Replace("'","#@dotnet.itags.org.").Trim() +"' , tbl_usr_info.address_line_1 = '" + aD1.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.address_line_2 = '" + aD2.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.address_line_3 = '" + aD3.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.post_code = '" + pCode.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.work_phone = '" + wPhone.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.home_phone = '" + hPhone.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' , tbl_usr_info.email_addy = '" + eAddy.Text.ToString().Replace("'","#@dotnet.itags.org.") +"' WHERE tbl_usr_info.usr_guid = '" + userGuid +"'", detCon); upComInfo.ExecuteNonQuery(); userNote.Text ="Your details have been updated"; }}


Thanks for your help in advance

Dan

your update_click is happening after the page_load. try changing the code you have in page_load to page_prerender and see if you are good to go.


i have treid to user it with IsPostBack but it doesnt work,

i didnt think that Page_load was re run when u clicked on a button and ran the function

Dan


Fixed it now thanks changed it to a !IsPostBack and it now works thanks

0 comments:

Post a Comment