显示用户帐户数据,然后更新到数据库 [英] displaying user account data and then update into database

查看:77
本文介绍了显示用户帐户数据,然后更新到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新用户帐户,这意味着当用户注册并继续使用其帐户时,我希望他能够通过单击更新按钮来更新其帐户,因此我使用了SqlDataReader rd = rd.sqldatareader;命令来检索数据从数据库中,并将这些数据显示到文本框中,并且在我运行它时成功完成了此操作.但是问题是,当我运行它并在文本框中进行更改时,而不是单击更新帐户的更新帐户时,它并没有更新我的帐户,而没有错误....

我的.cs文件代码如下:

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:

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"

推荐答案

在您的代码中,您没有使用IsPostBack 属性

因此请尝试以下代码:-
In your code you didn''t use IsPostBack property

so try below code:-
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";
        }


感谢Soution,它的工作原理是....但是为什么有必要....
Thanks for Soution, it Works.... but Why it is necessary....


这是必需的,因为Page_Load事件不仅会在第一次加载页面时发生,而且每次您单击页面中的控件时都会发生.

大多数控件使用回发"或回调"使服务器知道单击了某些内容.
发生这种情况时,页面将再次加载.
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.


这篇关于显示用户帐户数据,然后更新到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆