C#中,UPDATE问题

yee_yj 2010-02-09 05:14:19
问题如下:

我在用C#写新闻后台更新程序时,出现了个奇怪的问题:
首先我简单说明下我的思路:
我进入后台管理页面,是新闻的一个gridview,点击其中的修改,进入modify页面,该页面的各Textbox的值是从数据库中读取出来的,所以显示的时候是待修改的数据。但是修改完提交以后:
update语句中,不加where条件时,gridview中所有行都被重置为刚要修改的那一行,而且还是未修改前的数据;加了where条件后,更新就无效了

这个问题电脑没有提示任何错误消息,id传值正确,我测试过了


谢谢大侠么,拜谢
...全文
170 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangminqi 2010-02-10
  • 打赏
  • 举报
回复
看看 Request.QueryString["id"] 的值是否为空



http://www.mybuffet.cn
yee_yj 2010-02-10
  • 打赏
  • 举报
回复

page_load()
{
if(!ispostback)
{ your code }
}
yee_yj 2010-02-10
  • 打赏
  • 举报
回复
[code=c#]
page_load()
{
if(!ispostback)
{ your code }
}
[/code]
yee_yj 2010-02-10
  • 打赏
  • 举报
回复
引用 4 楼 seesea125 的回复:
page_load()
{
if(!ispostback)
{
your code
}
}


正解,基础没过关,没办法,新手一个,呵呵
yee_yj 2010-02-09
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace zjutieyjs.admin
{
public partial class modi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server='(local)';database='news';uid='sa';pwd='sa';");
conn.Open();
string str = "select * from news where id='" + Request.QueryString["id"] + "'";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.Read())
{
TextBox1.Text=rd["Title"].ToString();
TextBox2.Text=rd["Time"].ToString();
TextBox3.Text=rd["Author"].ToString();
DropDownList3.Text=rd["Weather"].ToString();
FCKeditor1.Value=rd["Content"].ToString();
DropDownList1.Text=rd["Level"].ToString();
DropDownList2.Text=rd["Classname"].ToString();
}
rd.Close();
conn.Close();

}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("server='(local)';database='news';uid='sa';pwd='sa';");
con.Open();
//string strsql = ame) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + FCKeditor1.Value + "','" + DropDownList3.Text + "','" + DropDownList1.Text + "','" + DropDownList2.Text + "')";
//string strsql = "UPDATE News SET Title = '" + TextBox1.Text + "',Time = '" + TextBox2.Text + "', Author = '" + TextBox3.Text + "', Content ='" + FCKeditor1.Value + "', Weather = '" + DropDownList3.Text + "', Level = '" + DropDownList1.Text + "',Classname = '" + DropDownList2.Text + "'where ID='" + Request.QueryString["id"] + "'";
string strsql = "UPDATE News SET Title = '" + TextBox1.Text + "'where ID='" + Request.QueryString["id"] + "'";
SqlCommand cmdd = new SqlCommand(strsql, con);
cmdd.ExecuteNonQuery();

StreamReader aw = File.OpenText(Server.MapPath("template.htm"));
string template = aw.ReadToEnd();
aw.Close();

template = template.Replace("[新闻标题]", TextBox1.Text);
template = template.Replace("[发布时间]", TextBox2.Text);
template = template.Replace("[新闻作者]", TextBox3.Text);
template = template.Replace("[新闻天气]", DropDownList3.Text);
template = template.Replace("[新闻内容]", FCKeditor1.Value);

StreamWriter sw = File.CreateText(Server.MapPath("../html/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + ".htm"));
sw.Write(template);
sw.Close();

Response.Redirect("manage.aspx");

}
catch (Exception ee)
{
Response.Write(ee.ToString());
}
}
}
}
yee_yj 2010-02-09
  • 打赏
  • 举报
回复
数据库里的值更新后也没变,不过添加新闻操作是对的
yee_yj 2010-02-09
  • 打赏
  • 举报
回复
能添加新闻的,就是更新有问题
seesea125 2010-02-09
  • 打赏
  • 举报
回复
page_load()
{
if(!ispostback)
{
your code
}
}
liuhongyu_cjzm 2010-02-09
  • 打赏
  • 举报
回复
修改完 跳回新闻页面,重新绑定一下 ,如果不行的话 你看看modify这个页面修改是否成功
调试一下看看~!
fangyuantdy 2010-02-09
  • 打赏
  • 举报
回复
修改完没有重新检索数据库绑定到GridView上?
lijing3333 2010-02-09
  • 打赏
  • 举报
回复
你数据库里面的值改了么? gridview修改后要从新绑定到页面才能显示出来的

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧