SAM
SAM

Reputation: 39

problem in updating the textbox data into SQLSERVER

I have problem in updating user profile through asp:TextBox. At the time of page load , all the textboxes get loaded with correct values from the database sqlserver.

say First name=SAM {i.e SAM is shown in txtName's textfield) and Last Name=Berton

but when i alter the content of txtName to SAMANTHA and similary for other textboxes, and click on update profile button, Internally txtName.text="SAM" gets retained notwithstanding the changed content of txtName.text="SAMANTHA" by manually altering the content of textbox.So same value that is "SAM" gets stored into the sqlserver rather than "SAMANTHA".

protected void Page_Load(object sender, EventArgs e)
    {

        loadProfileData();
    }
protected void loadProfileData()
    {

        string connStringProfileload = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
        SqlConnection conProfLoad = new SqlConnection(connStringProfileload);
        conProfLoad.Open();
        string emailAddLogin = User.Identity.Name.ToString();
        string strSqlProfileLoad = "SELECT * FROM [gen_profile] WHERE [email]=@email";

        SqlCommand cmd = new SqlCommand(strSqlProfileLoad, conProfLoad);
        cmd.Parameters.AddWithValue("@email", emailAddLogin);
        SqlDataReader drProfileLoad = cmd.ExecuteReader();
        while (drProfileLoad.Read())
        {
            txtName.Text = drProfileLoad["fname"].ToString();
            txtLastName.Text=drProfileLoad["lname"].ToString();


        }

        drProfileLoad.Close();
        conProfLoad.Close();
    }

protected void BtnUpdtProf_Click(object sender, EventArgs e)
    {
        string connStringProfileUpdate = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
        SqlConnection conUpdateProf = new SqlConnection(connStringProfileUpdate);
        conUpdateProf.Open();
        string emailAddLogin = User.Identity.Name.ToString();
        string strSqlUpdateProf = "UPDATE  gen_profile SET fname =@fname, lname =@lname where email=@email;";

        SqlCommand cmdUpdate = new SqlCommand(strSqlUpdateProf, conUpdateProf);
        cmdUpdate.Parameters.AddWithValue("@fname", txtName.Text.ToUpper().Trim());
        cmdUpdate.Parameters.AddWithValue("@lname", txtLastName.Text.ToUpper().Trim());

        cmdUpdate.Parameters.AddWithValue("@email", emailAddLogin);



        int i = cmdUpdate.ExecuteNonQuery();
        conUpdateProf.Close();
        if (i == 1)
            Response.Redirect("~/prof/resp_update_prof.aspx");
    }

Upvotes: 0

Views: 632

Answers (2)

Fishcake
Fishcake

Reputation: 10774

This is because before your button click event, page_load get's fired which re-populates the textbox.

You can stop this happening by checking for a post back as follows:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) 
    {
        loadProfileData();
    }
}

Upvotes: 0

Dave Long
Dave Long

Reputation: 794

Change this method :

protected void Page_Load(object sender, EventArgs e) {
    loadProfileData();
}

to :

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) loadProfileData();
}

Basically, your textBox values are being reset in the page load event, so everytime your page posts back, it's resetting the values in the textboxes and then saving them back to the database.

Upvotes: 3

Related Questions