int index = int.Parse(Request.QueryString["index"]);
try
{
connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString.ToString());
connection.Open();
command = new SqlCommand("update [filling] set question='" + this.TextBox1.Text + "', answer='" + this.TextBox2.Text + "', belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " where ID=" + index, connection);
int lines = command.ExecuteNonQuery();
}
catch (Exception err)
{
Response.Write(err.ToString());
}
finally
{
connection.Close();
}
这样写有错误码?为什么老是更新不了?????
try
{
connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString.ToString());
connection.Open();
command = new SqlCommand("update [filling] set question='" + this.TextBox1.Text + "', answer='" + this.TextBox2.Text + "', belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " where ID=" + index, connection);
int lines = command.ExecuteNonQuery();
}
catch (Exception err)
{
Response.Write(err.ToString());
}
finally
{
connection.Close();
}
这样写有错误码?为什么老是更新不了?????
解决方案 »
- 关于数组的问题!!~!
- 搜索框--跟百度差不多
- Repeater嵌套的RadioButtonList SelectedIndexChanged事件
- 生成静态的网站目录权限问题
- asp.net动态生成js文件合理么?
- 关于页面弹出个div的问题?
- 菜鸟提问:ASp.net 如何动态连接 多个(3个) 数据库
- [高分在线]如何从DataTable中过滤出一个新的DataTable
- 简单问题,请各位大哥大姐路过的进来看一下哦!
- ACT测试的一个奇怪问题
- #进程死锁的问题,高手请进#
- 上传图片提示这样的错误:"A generic error occurred in GDI+." Description: An unhandled exception occurred during
{
这里是页面加载的时候所要加载的数据
}
加这上个
点击保存按钮的时候运行下面的代码protected void Button1_Click(object sender, EventArgs e)
{
if (this.Button1.Text == "保存")
{
int index = int.Parse(Request.QueryString["index"]);
try
{
connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString.ToString());
connection.Open();
command = new SqlCommand("update [filling] set question='" + this.TextBox1.Text + "', answer='" + this.TextBox2.Text + "', belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " where ID=" + index, connection);
int lines = command.ExecuteNonQuery();
}
catch (Exception err)
{
Response.Write(err.ToString());
}
finally
{
connection.Close();
}
}
}
这一句话的时候,sql语句执行了没有呢?我一直不知道sql语句是什么时候执行的
这一句在执行时建议楼主判断一下,Request.QueryString["index"]是不是为空呀,会报错的!!!
建议楼主看一下,index是不是0呀?
第二:确保可以获取到参数;
第三:确保sql语句写的正确;应该就没有问题了,sql 建议用string.format()来写,清晰易懂安全。
加俩个单引号试试 belongto='" + int.Parse(this.DropDownList1.SelectedValue) + "'
我用一个textbox把sql语句打印出来,发现一个问题,就是this.textbox1.text输出来的不是我最新输入的数据,而是原来的旧的数据,为什么会这样子呢?
1)把每个参数都打出来看看,然后注意以下 belongto,id的类型,如果是char型最好加引号。
2)id的长度和你传递过来的长度是否相等呢?比如id为char(4)行,传递过来的id是'012'这个时候进行查询需要在最后补一个空格 id = '012 '
如果是这样的话,问题就简单多了,你出的问题应该在Page_Load()
这个地方,把你page_Load()都放在
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ }
}
{ }
如果你没有加这个的话。那么你更新的数据就还是你第一次绑定的值。所以把你绑定的那些值的方法写到 if (!Page.IsPostBack)
{
//绑定加载方法
}
{
if (!Page.IsPostBack)
{
bindData(); //绑定数据的过程
}
}因为只要提交了页面,就会刷新页面,就会触发Page_Load(),而导致页面重新绑定,所以更新的数据也就是重新绑定后的数据了。而用IsPostBack就可以判断是否是因为回发导致的页面刷新,这样无论刷新多少次页面也只绑定一次数据了。
成功了 明白了
意思也就是说 当按下页面中的控件导致postback发生的时候 我们不需要重新读取数据库 那么我们可以通过if (!Page.IsPostBack)来区分。谢谢楼上的所有高手!!!