用ASP.NET编写网页连接SQL数据库时,在Page_Load里从数据库读出信息然后在点击按钮更新数据库不成功

protectedvoidPage_Load(objectsender,EventArgse){SqlConnectioncn=newSqlConnection(Comm... protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn= new SqlConnection(Common.Class1.ConnectString);
string sql="select username,password,name,age,sex,phone,school,xueyuan,zhuanye,email from student where username='"+ this.Session["username"] +"'";
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds=new DataSet();
cn.Open();
da.Fill(ds, "xinxi");
this.username.Text = ds.Tables["xinxi"].Rows[0]["username"].ToString();
this.password.Text = ds.Tables["xinxi"].Rows[0]["password"].ToString();
this.name.Text = ds.Tables["xinxi"].Rows[0]["name"].ToString();
this.age.Text = ds.Tables["xinxi"].Rows[0]["age"].ToString();
this.sex.Text = ds.Tables["xinxi"].Rows[0]["sex"].ToString();
this.phone.Text = ds.Tables["xinxi"].Rows[0]["phone"].ToString();
this.school.Text = ds.Tables["xinxi"].Rows[0]["school"].ToString();
this.xueyuan.Text = ds.Tables["xinxi"].Rows[0]["xueyuan"].ToString();
this.zhuanye.Text = ds.Tables["xinxi"].Rows[0]["zhuanye"].ToString();
this.email.Text = ds.Tables["xinxi"].Rows[0]["email"].ToString();
cn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = this.username.Text;
string password = this.password.Text;
string name = this.name.Text;
string age = this.age.Text;
string sex = this.sex.Text;
string phone = this.phone.Text;
string school = this.school.Text;
string xueyuan = this.xueyuan.Text;
string zhuanye = this.zhuanye.Text;
string email = this.email.Text;
SqlConnection cn = new SqlConnection(Common.Class1.ConnectString);
string update="update student set username='"+ username +"',password='"+ password +"',name='"+ name +"',age='"+ age +"',sex='"+ sex +"',phone='"+ phone +"',school='"+ school +"',xueyuan='"+ xueyuan +"',zhuanye='"+ zhuanye +"',email='"+ email +"' where username='"+ this.Session["username"] +"'";
//string update = "update student set password='" + password + "',phone='" + phone + "'where username='" + this.Session["username"] + "'";
SqlCommand cmd = new SqlCommand(update, cn);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Response.Redirect("学生登陆页面.aspx");
}

如果将Page_Load注释掉能正常更新但是同时运行就无法更新数据库为什么呢?
展开
 我来答
桂慕思0IW
2013-02-24 · TA获得超过433个赞
知道小有建树答主
回答量:229
采纳率:0%
帮助的人:142万
展开全部
if(!IsPostBack)
{
把你pageLoad代码写在这个if里面
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式