Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to update a user account, i mean when a user signed up and continue with its account than i wish that he is capable to Update its account with clicking update button, so i used SqlDataReader rd = rd.sqldatareader; command to retrive data from database, and displays these data into textboxs, and i am successed to do this when i run it. But problem is that when i run it and doing changes in textboxes and than click update button for update account it is not updating my account witout error....

my code of .cs file is below:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class updateaccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) //for dsplay usr data in textboxes
    {
        SqlConnection con = new SqlConnection("server=.\\SQLEXPRESS; AttachDbFileName=|DataDirectory|\\Database.mdf; user instance=true; trusted_connection=yes");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM signup WHERE id='"+(string)Session["User"]+"'",con);    //here Session user ='user id'
        SqlDataReader rd = cmd.ExecuteReader();
        
        if (rd.Read())
        {
             TextBox1.Text = rd.GetString(0).ToString();
             TextBox2.Text = rd.GetString(1).ToString();
             TextBox3.Text = rd.GetString(2).ToString();
             TextBox4.Text = rd.GetString(5).ToString();
             TextBox5.Text = rd.GetString(6).ToString();
             DropDownList1.SelectedValue = rd.GetString(7).ToString();
             TextBox6.Text = rd.GetString(8).ToString();
        }
        

    }

protected void Button1_Click(object sender, EventArgs e) //for updtng usr acount
    {
        SqlConnection con = new SqlConnection("server=.\\SQLEXPRESS; AttachDbFileName=|DataDirectory|\\Database.mdf; user instance=true; trusted_connection=yes");
        con.Open();
        SqlCommand cmd = new SqlCommand("UPDATE signup SET fn = '"+ this.TextBox1.Text + "', ln = '" + this.TextBox2.Text + "', emailid= '" + this.TextBox4.Text + "', address = '" + this.TextBox5.Text + "', scquestion = '" + this.DropDownList1.SelectedValue + "', answer = '"+ this.TextBox6.Text +"' WHERE id = '"+ (string)Session["User"] +"' ",con);
        SqlDataReader rd = cmd.ExecuteReader();
        con.Close();
        Label3.Text = "Update successfully";
    }
}      //this code not updating my account rather it Prints "Update successful"
Posted
Updated 17-Jun-12 2:01am
v2

In your code you didn't use IsPostBack property

so try below code:-
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection("server=.\\SQLEXPRESS; AttachDbFileName=|DataDirectory|\\Database.mdf; user instance=true; trusted_connection=yes");
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM signup WHERE id='" + (string)Session["User"] + "'", con); //here Session user ='user id'
                SqlDataReader rd = cmd.ExecuteReader();

                if (rd.Read())
                {
                    TextBox1.Text = rd.GetString(0).ToString();
                    TextBox2.Text = rd.GetString(1).ToString();
                    TextBox3.Text = rd.GetString(2).ToString();
                    TextBox4.Text = rd.GetString(5).ToString();
                    TextBox5.Text = rd.GetString(6).ToString();
                    DropDownList1.SelectedValue = rd.GetString(7).ToString();
                    TextBox6.Text = rd.GetString(8).ToString();
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.\\SQLEXPRESS; AttachDbFileName=|DataDirectory|\\Database.mdf; user instance=true; trusted_connection=yes");
            con.Open();
            SqlCommand cmd = new SqlCommand("UPDATE signup SET fn = '" + this.TextBox1.Text + "', ln = '" + this.TextBox2.Text + "', emailid= '" + this.TextBox4.Text + "', address = '" + this.TextBox5.Text + "', scquestion = '" + this.DropDownList1.SelectedValue + "', answer = '" + this.TextBox6.Text + "' WHERE id = '" + (string)Session["User"] + "' ", con);
            SqlDataReader rd = cmd.ExecuteReader();
            con.Close();
            Label3.Text = "Update successfully";
        }
 
Share this answer
 
v2
Thanks for Soution, it Works.... but Why it is necessary....
 
Share this answer
 
It is necessary because the Page_Load event not only occurs on the first page load but each time you click on a control in the page.

Most controls use 'postback' or 'callback' to make the server aware that something is clicked.
When this happen the page is loaded again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900