开发者

textbox value remains unchanged on form submission in edit profile page

i am trying to create a edit profile page so when the page loads first i am filling all text boxes values as the value present in the database for the particular user.i am doing this using the code:

        DBconn obj1 = new DBconn();
        String sql1 = "select* from users where user_id='" + Session["user_id"].ToString() + "'";
        DataTable dt = new DataTable();
        dt = obj1.getdatatable(sql1);
        if (dt.Rows.Count > 0)
        {
            referal_id.Text = dt.Rows[0]["referal_id"].ToString();
            name.Text = dt.Rows[0]["name"].ToString();
            password.Text = dt.Rows[0]["password"].ToString();
            email.Text = dt.Rows[0]["email"].ToString();
            mobile.Text = dt.Rows[0]["mobile"].ToString();
            city.Text = dt.Rows[0]["city"].ToString();
            state.Text = dt.Rows[0]["state"].ToString();
            pincode.Text = dt.Rows[0]["pincode"].ToString();
            sec_ques.Text = dt.Rows[0]["securityq"].ToString();
          开发者_如何学JAVA  answer.Text = dt.Rows[0]["answer"].ToString();
            age.Text = dt.Rows[0]["age"].ToString();
            gender.Text = dt.Rows[0]["gender"].ToString();
            user_id.Text = dt.Rows[0]["user_id"].ToString();
            address.Text = dt.Rows[0]["address"].ToString();
            date_of_joining.Text = dt.Rows[0]["date_of_joining"].ToString();
            user_type.Text = dt.Rows[0]["user_type"].ToString();
        }

now this is filling all the text boxes with the values . when user edits some values in some textbox and resubmits the form the database is getting updated implementing the code:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    int i;
    DBconn obj2 = new DBconn();
    String sql2 = "update users set name='"+name.Text+"',address='"+address.Text+"',age='"+age.Text+"',gender='"+gender.Text+"',email='"+email.Text+"',mobile='"+mobile.Text+"',securityq='"+sec_ques.Text+"',answer='"+answer.Text+"',city='"+city.Text+"',state='"+state.Text+"',pincode='"+pincode.Text+"' where user_id='"+Session["user_id"].ToString()+"'";
    i = obj2.executeDML(sql2);
    if (i > 0)
    {
        Label1.Visible = true;
        Image2.Visible = true;
        Label1.Text = "Updated successfully!";

    }
    else
    {
        Label1.Visible = true;

        Label1.Text = "Oops Update was unsuccessfull!";
    }

}

the problem is the database is getting updated with the previous values which was already present in the database.when i used watchpoints i found the sql statement gets loaded with the textbox values which i binded using the above database code but its not fetching the values which the user edits.

plz help.


Hey Robin, Please check whether the code is placed under the !IsPostback condition or not.

Please bind the values under this if loop.

    if(!IsPostback)
    {

    // Bind values

    }

do this on the page load.


The values must only be bound for the first time the page is load.

In your case it seems the values to the text boxes are bound for each and every time it is loaded(even in the postback also the values are loaded.)

So, in page load you must check the condition whether the page is postback or not.

Thus add this condition while binding the values in page load.

 if(!IsPostback)      


Firstly check the location from where u r calling the mathod for binding values...

put it on

Page.isPostback

so it will not change the value at posting time then when u will save it the value will change

the value must be changing when u click on button and page is again loading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜