protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["UserName"] == null || Session["UserName"].ToString() == "")
{
Response.Write("<script>alert('未登陆或登陆超时');window.location.href='Default.aspx'</script>");
}
ddlclass.DataSource = new Sqlhelp().GetDataSet("select * from class");
ddlclass.DataTextField = "ClassName";
ddlclass.DataValueField = "Cid";
ddlclass.DataBind();
if (Request.QueryString ["id"] == null || Request.QueryString ["id"].ToString ()=="")
{ }
else
{
//取得要修改的新闻编号
string newsID = Request.Params["id"].ToString();
SqlDataReader dr = new Sqlhelp().GetReader("select * from news where ID=" + newsID);
if (dr.Read())
//显示新闻内容
{
txttitle.Text = dr["Title"].ToString();
TContent.Text = dr["Content"].ToString();
txtauthor.Text = dr["Author"].ToString();
}
}
} }
protected void btncannel_Click(object sender, EventArgs e)
{
txttitle.Text = "";
TContent.Text = "";
txtauthor.Text = "";
}
protected void btnok_Click(object sender, EventArgs e)
{
//检查用户输入是否合法
if (txttitle.Text == "" || TContent.Text == "")
{
Label1.Text = "标题,内容不能为空!";
return;
}
//保存上传文件
String filepath = Server.MapPath("..\\upload\\" +Path.GetFileName(File1.PostedFile.FileName));
if (File.Exists(filepath))
{
Response.Write("<script language=javascript>alert('上传文件重名,请改名后再上传!');<script>");
return;
}
else
{
if (File1.Value.ToString() != "")
{
File1.PostedFile.SaveAs(filepath);
}
}
String sql;
SqlConnection Conn = new Sqlhelp().GetConnStr();
sql = "update news set Title='" + txttitle.Text + "',Content='" + TContent.Text + "',Author='" + txtauthor.Text + "',Img='" + "upload/" + Path.GetFileName(File1.PostedFile.FileName) + "',Newstime='" + DateTime.Now.ToString(); +"',Classname='" + ddlclass.SelectedItem.Value + "'where ID='"+newsID;
SqlCommand cmd = new SqlCommand(sql, Conn);
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = txttitle.Text;
cmd.Parameters.Add("@Content", SqlDbType.VarChar).Value = TContent.Text;
cmd.Parameters.Add("@Author", SqlDbType.VarChar).Value = txtauthor.Text;
cmd.Parameters.Add("@Click", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@Img", SqlDbType.VarChar).Value = "upload/" + Path.GetFileName(File1.PostedFile.FileName);
cmd.Parameters.Add("@Newstime", SqlDbType.DateTime).Value = DateTime.Now.ToString();
cmd.Parameters.Add("@ClassName", SqlDbType.Int).Value = ddlclass.SelectedItem.Value;
int ret = cmd.ExecuteNonQuery();
if (ret > 0)
{
Response.Write("<script>window.alert('成功加入')</script>");
}
}
就是红色一句报错,要怎么修改
{
if (!IsPostBack)
{
if (Session["UserName"] == null || Session["UserName"].ToString() == "")
{
Response.Write("<script>alert('未登陆或登陆超时');window.location.href='Default.aspx'</script>");
}
ddlclass.DataSource = new Sqlhelp().GetDataSet("select * from class");
ddlclass.DataTextField = "ClassName";
ddlclass.DataValueField = "Cid";
ddlclass.DataBind();
if (Request.QueryString ["id"] == null || Request.QueryString ["id"].ToString ()=="")
{ }
else
{
//取得要修改的新闻编号
string newsID = Request.Params["id"].ToString();
SqlDataReader dr = new Sqlhelp().GetReader("select * from news where ID=" + newsID);
if (dr.Read())
//显示新闻内容
{
txttitle.Text = dr["Title"].ToString();
TContent.Text = dr["Content"].ToString();
txtauthor.Text = dr["Author"].ToString();
}
}
} }
protected void btncannel_Click(object sender, EventArgs e)
{
txttitle.Text = "";
TContent.Text = "";
txtauthor.Text = "";
}
protected void btnok_Click(object sender, EventArgs e)
{
//检查用户输入是否合法
if (txttitle.Text == "" || TContent.Text == "")
{
Label1.Text = "标题,内容不能为空!";
return;
}
//保存上传文件
String filepath = Server.MapPath("..\\upload\\" +Path.GetFileName(File1.PostedFile.FileName));
if (File.Exists(filepath))
{
Response.Write("<script language=javascript>alert('上传文件重名,请改名后再上传!');<script>");
return;
}
else
{
if (File1.Value.ToString() != "")
{
File1.PostedFile.SaveAs(filepath);
}
}
String sql;
SqlConnection Conn = new Sqlhelp().GetConnStr();
sql = "update news set Title='" + txttitle.Text + "',Content='" + TContent.Text + "',Author='" + txtauthor.Text + "',Img='" + "upload/" + Path.GetFileName(File1.PostedFile.FileName) + "',Newstime='" + DateTime.Now.ToString(); +"',Classname='" + ddlclass.SelectedItem.Value + "'where ID='"+newsID;
SqlCommand cmd = new SqlCommand(sql, Conn);
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = txttitle.Text;
cmd.Parameters.Add("@Content", SqlDbType.VarChar).Value = TContent.Text;
cmd.Parameters.Add("@Author", SqlDbType.VarChar).Value = txtauthor.Text;
cmd.Parameters.Add("@Click", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@Img", SqlDbType.VarChar).Value = "upload/" + Path.GetFileName(File1.PostedFile.FileName);
cmd.Parameters.Add("@Newstime", SqlDbType.DateTime).Value = DateTime.Now.ToString();
cmd.Parameters.Add("@ClassName", SqlDbType.Int).Value = ddlclass.SelectedItem.Value;
int ret = cmd.ExecuteNonQuery();
if (ret > 0)
{
Response.Write("<script>window.alert('成功加入')</script>");
}
}
就是红色一句报错,要怎么修改
解决方案 »
- 跪求高手啊,我做了两个页面,一个有gridview,我想在点击gridview的一个超链接列时,把点击的这一行的数据传到下一个页面。跪求啊~~~~~
- 多维数组保存数据问题!
- DataList 的取值问题
- ASPX切换ascx问题
- 问一题数据库题目!
- Visual Studio 2005 Web Application Projects支持中文版的发布了吗????
- 谁有 .NET 2005 数据库操作封装类方面的资料啊??
- ***************请问关于js创建动态表格的奇怪问题****************
- 这句sql老报错!!!郁闷
- 这段代码错了需要理由吗??不需要!!!(连接sql数据库的)
- 关于视频播放的问题。
- 请教高手: 点击验证码不能实现刷新,报缺少对象的错误
这一句
你语句中都没有@Title这个。
改成
update news set Title=@Title,111=@111,222=@222.....
这样子,不能用字符串连接。
sql = "update news set Title=@Title,Content=@Content,Author=@Author,Img=@Img,Newstime=@Newstime,Classname=@Classname where ID='"+newsID;
当前上下文中不存在名称“newsID”