页面上放一个TEXTBOX txtCollege,一个BUTTON,根据前面页传来ID,查询出一条记录,txtCollege中为查询出的某一字段的值,string sql="SELECT * FROM College WHERE ID="+request["id"].tostring();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
try
{
cmd.ExecuteReader();
txtCollege.text=dr["College"].tostring();
}
catch (SqlException ex)
{
cn.Close();
throw new Exception(ex.Message);
}
if (cmd != null)
cmd.Dispose();
}
}
修改txtCollege可的信息,点击BUTTON进行修改,修改后,为什么College的值并没有变化,跟踪了一下发现txtCollege.Text是修改前的,我明明修改了,为什么提交到数据库里的值是修改前的呢?直接这样写的
int reg=0;
string sql = "UPDATE College SET College='" + txtCollege.Text + "' WHERE ID=" + Request["ID"].ToString();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
try
{
ret=cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
cn.Close();
throw new Exception(ex.Message);
}
if (cmd != null)
cmd.Dispose();
}
}
if (ret!=0)
response.write("<script language=javascript>alert('成功');</script>");
else
response.write("<script language=javascript>alert('失败');</script>");
然后提示修改成功
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
try
{
cmd.ExecuteReader();
txtCollege.text=dr["College"].tostring();
}
catch (SqlException ex)
{
cn.Close();
throw new Exception(ex.Message);
}
if (cmd != null)
cmd.Dispose();
}
}
修改txtCollege可的信息,点击BUTTON进行修改,修改后,为什么College的值并没有变化,跟踪了一下发现txtCollege.Text是修改前的,我明明修改了,为什么提交到数据库里的值是修改前的呢?直接这样写的
int reg=0;
string sql = "UPDATE College SET College='" + txtCollege.Text + "' WHERE ID=" + Request["ID"].ToString();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
try
{
ret=cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
cn.Close();
throw new Exception(ex.Message);
}
if (cmd != null)
cmd.Dispose();
}
}
if (ret!=0)
response.write("<script language=javascript>alert('成功');</script>");
else
response.write("<script language=javascript>alert('失败');</script>");
然后提示修改成功
解决方案 »
- 如何向父窗口赋值?
- HTTP 错误 500.100 - 内部服务器错误 - ASP 错误
- 如果所需的注册的内容和CreateUserWizard控件中提供的不同?
- 请教GridView如何在代码中添加 模板列 ????????谢谢。。。。。。。。。。。
- Session和Cookie问题
- 修改一条记录用什么方法好?
- if(rd.read()) ????????????????
- 我设置了断点为什么运行时不能中断,断点没有起作用?
- 登陆怎么也登陆不进去,总提示密码错误,高手给看看
- access在任何系统环境的虚拟主机上都能运行么。。
- 【大蝦SOS】petshop4 中sqlhelper為什么是抽象類
- asp.net中怎么把富文本框里面的内容保存到数据库?
{
//绑定数据
}
{
if (!Page.IsPostBack)
{string sql="SELECT * FROM College WHERE ID="+Request["id"].tostring();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
try
{
cmd.ExecuteReader();
txtCollege.text=dr["College"].tostring();
}
catch (SqlException ex)
{
cn.Close();
throw new Exception(ex.Message);
}
if (cmd != null)
cmd.Dispose();
}
} }
}