新手,求大神解答!
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 58: string txt = ftb.Text;
行 59: string newstime = Request.Form["newstime"];
行 60: string txt1 = newstime.Substring(0, 4) + "/" + newstime.Substring(5, 1) + "/" + newstime.Substring(8, 1);
行 61: string strInsert = "insert into news_article(bigclassid,title,writer,newskey,newstime,content,newsfrom)values('" + Request.Form["bigclass"] + "','" + Request.Form["title1"].Trim() + "','" + Request.Form["writer1"].Trim() + "','" + Request.Form["newskey1"].Trim() + "','" + txt1 + "','" + txt + "','" + Request.Form["newsfrom1"].Trim() + "')";
行 62: SqlCommand commInsert = new SqlCommand(strInsert, conn);
源文件: c:\Users\123\第11章\add_article.aspx.cs 行: 60
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
add_article.Button1_Click(Object sender, EventArgs e) in c:\Users\123\第11章\add_article.aspx.cs:60
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9692746
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class add_article : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null || Session["password"] == null)
{
Response.Redirect("login.aspx");
}
string rqid = Request.QueryString["newid"];
if (rqid != null)
{
Button1.Visible = false;
string strSql = "select newid,bigclassname,title,newsfrom,newstime,writer,hit,newskey,content from news_article inner join news_bigclass on news_article.bigclassid=news_bigclass.bigclassid where newid='" + rqid + "' order by newstime desc";
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
title1.Text = dr["title"].ToString();
newskey1.Text = dr["newskey"].ToString();
newsfrom1.Text = dr["newsfrom"].ToString();
writer1.Text = dr["writer"].ToString();
ftb.Text = dr["content"].ToString();
newstime.Text = dr["newstime"].ToString();
}
conn.Close();
}
else
{
newstime.Text = DateTime.Now.ToString();
Button2.Visible = false;
}
string strLeiBie = "select * from news_bigclass";
conn.Open();
SqlCommand commLeiBie = new SqlCommand(strLeiBie, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = commLeiBie;
DataSet ds = new DataSet();
da.Fill(ds, "leibie");
bigclass.DataSource = ds.Tables["leibie"];
bigclass.DataBind();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
conn.Open();
string txt = ftb.Text;
string newstime = Request.Form["newstime"];
string txt1 = newstime.Substring(0, 4) + "/" + newstime.Substring(5, 1) + "/" + newstime.Substring(8, 1);
string strInsert = "insert into news_article(bigclassid,title,writer,newskey,newstime,content,newsfrom)values('" + Request.Form["bigclass"] + "','" + Request.Form["title1"].Trim() + "','" + Request.Form["writer1"].Trim() + "','" + Request.Form["newskey1"].Trim() + "','" + txt1 + "','" + txt + "','" + Request.Form["newsfrom1"].Trim() + "')";
SqlCommand commInsert = new SqlCommand(strInsert, conn);
commInsert.ExecuteNonQuery();
conn.Close();
Response.Write("alert('成功添加新闻信息!');windows.history.go(-1);");
}
protected void Button2_Click(object sender, EventArgs e)
{
conn.Open();
string txt = ftb.Text;
string newstime = Request.Form["newstime"];
string txt1 = newstime.Substring(0, 4) + "/" + newstime.Substring(5, 1) + "/" + newstime.Substring(8, 1);
string strUpdate = "update news_article set title='" + Request.Form["title1"].Trim() + "',bigclassid='" + Request.Form["bigclass"] + "',writer='" + Request.Form["writer1"].Trim() + "',newskey='" + Request.Form["newskey1"].Trim() + "',newsfrom='" + Request.Form["newsfrom1"].Trim() + "',newstime='" + txt1 + "',content='" + txt + "'where newid='" + Request.QueryString["newid"] + "'";
SqlCommand commUpdate = new SqlCommand(strUpdate, conn);
commUpdate.ExecuteNonQuery();
conn.Close();
Response.Write("alert('成功修改新闻信息!');windows.history.go(-1);");
}
}