就是不能更新?程序和SQL语句都没错
我的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace user
{
/// <summary>
/// rework 的摘要说明。
/// </summary>
public class rework : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label12;
protected System.Web.UI.WebControls.Label lmessage;
protected System.Web.UI.WebControls.TextBox tname;
protected System.Web.UI.WebControls.TextBox tyw;
protected System.Web.UI.WebControls.TextBox tdz;
protected System.Web.UI.WebControls.TextBox tdh;
protected System.Web.UI.WebControls.TextBox tcz;
protected System.Web.UI.WebControls.TextBox tyb;
protected System.Web.UI.WebControls.TextBox tmail;
protected System.Web.UI.WebControls.TextBox twz;
protected System.Web.UI.WebControls.TextBox tff;
protected System.Web.UI.WebControls.TextBox tzp;
protected System.Web.UI.WebControls.TextBox tjj;
protected ClassConn myconn;
string id;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
id=Request["myid"].ToString();
myconn=new ClassConn();
string sql="select * from pds.corp";
DataSet ds=new DataSet();
ds=myconn.DataSets(sql);
tname.Text= Convert.ToString(ds.Tables[0].Rows[0]["corp_name"]);
tyw.Text= Convert.ToString(ds.Tables[0].Rows[0]["main_business"]);
tdz.Text= Convert.ToString(ds.Tables[0].Rows[0]["corp_addr"]);
tdh.Text= Convert.ToString(ds.Tables[0].Rows[0]["corp_phone"]);
tcz.Text= Convert.ToString(ds.Tables[0].Rows[0]["fax"]);
tyb.Text= Convert.ToString(ds.Tables[0].Rows[0]["post_code"]);
tmail.Text= Convert.ToString(ds.Tables[0].Rows[0]["corp_email"]);
twz.Text= Convert.ToString(ds.Tables[0].Rows[0]["website"]);
tff.Text= Convert.ToString(ds.Tables[0].Rows[0]["fee_phone"]);
tzp.Text= Convert.ToString(ds.Tables[0].Rows[0]["corp_picture"]);
tjj.Text= Convert.ToString(ds.Tables[0].Rows[0]["brief"]);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
myconn=new ClassConn();
string sql="update pds.corp set crop_name='"+tname.Text+"',main_business='"+tyw.Text+"',corp_addr='"+tdz.Text+"',corp_phone='"+tdh.Text+"',fax='"+tcz.Text+"',post_code='"+tyb.Text+"',corp_email='"+tmail.Text+"',website='"+twz.Text+"',fee_phone='"+tff.Text+"',corp_picture='"+tzp.Text+"',brief='"+tjj.Text+"' where corp_id='"+id+"'";
myconn.execsql(sql);
myconn.CloseDB();
lmessage.Text="更新成功请返回.";
}
catch
{
lmessage.Text="更新失败请与管理员联系!";
}
}
}
}
====
ClassConn.cs的代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace user
{
/// <summary>
/// ClassConn 的摘要说明。
/// </summary>
public class ClassConn
{
public ClassConn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection connection;
public void OpenDB()
{
connection= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connstring"]);
connection.Open();
}
public void CloseDB()
{
connection.Dispose();
connection.Close();
}
public void execsql(string sql)
{
OpenDB();
SqlCommand cmd=new SqlCommand(sql,connection);
cmd.ExecuteNonQuery();
CloseDB();
}
public DataSet DataSets(string query)
{
OpenDB();
SqlDataAdapter rs=new SqlDataAdapter(query,connection);
DataSet ds=new DataSet();
rs.Fill(ds);
return ds;
}
}
}
问题点数:20、回复次数:10Top
1 楼TinKyNerd(ooO==QQ群:15686350 ASP.NET交流啊==Ooo)回复于 2005-04-24 09:25:57 得分 0
我不知道那里错了 我晕 我找了半天也没找到 大家看看Top
2 楼hchxxzx(NET?摸到一点门槛)回复于 2005-04-24 09:40:59 得分 5
你这个主要是在加载数据的时候,没有把加载的过程放在if(!this.ispostback)里面进行,导致更新时,每次都重新加载数据,不论你填写什么,它总是回到原来的数据,所以你就没有更新了.
改写如下:
if(!this.ispostback)
{
// 在此处放置用户代码以初始化页面
id=Request["myid"].ToString();
myconn=new ClassConn();
string sql="select * from pds.corp";
DataSet ds=new DataSet();
//赋值
....
....
}Top
3 楼kaigood(锴锴)回复于 2005-04-24 09:47:41 得分 0
把更新语句调试一下,在数据库中执行一遍看看,应该会有错误提示Top
4 楼hackate(兰花开香入梦境,独思佳人亦飘然!!)回复于 2005-04-24 10:22:18 得分 0
把拌定数据写在page_load
if(!IsPostBack)
{
//.拌定数据
}Top
5 楼nstorm(网の风)回复于 2005-04-24 10:32:40 得分 0
绑定数据要放到,否则每次都更新成绑定数据,你改变textbox值也没有用
if(!IsPostBack)
{
}Top
6 楼TinKyNerd(ooO==QQ群:15686350 ASP.NET交流啊==Ooo)回复于 2005-04-24 12:03:50 得分 0
把更新语句调试一下,在数据库中执行一遍看看,应该会有错误提示
我调试了没问题
if(!this.ispostback)
也不可以Top
7 楼hchxxzx(NET?摸到一点门槛)回复于 2005-04-24 12:45:52 得分 5
调度没有问题,那你把SQL语句输出后在数据库中直接执行看看.
你确信更新语句执行了吗?
问题是你上面的程序明显是因为没有写在ispostback中才引起的,如果加了以后仍然没有正确更新,那肯定是你的语句有问题Top
8 楼hendry_huang(MISA)回复于 2005-04-24 14:15:59 得分 0
是不能运行,还是可以运行但没达到要得结果?Top
9 楼fanweiwei(黑暗凝聚力量,堕落方能自由)回复于 2005-04-24 14:48:15 得分 5
先更新一下按F5刷新一下如有用!if(!this.ispostback)
一下就可以了Top
10 楼keliu8866(风中的人)回复于 2005-04-24 16:11:41 得分 5
可以确定的是由于你在page_load事件由于没有在确定是否是回传的情况下而绑定了数据,造成文本框的数据总在page_load事件中得回数据库中的数据,从而导致虽然总做更新,但看到数据库中的数据一直没变这样的效果,让你误认为没有做更新。
所以这里建议你在第一次加载页面的时候进行数据绑定Top




