black-han 2016-06-04 18:24 采纳率: 100%
浏览 1926
已采纳

未将对象引用设置到对象的实例--新手,求大神解答!

新手,求大神解答!
未将对象引用设置到对象的实例。
说明: 执行当前 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);");
}
}

  • 写回答

5条回答 默认 最新

  • 物是人非AUV 2016-06-05 05:44
    关注

    这还要搞这么复杂...
    第60行newstime为null,原因是59行的Request.Form["newstime"]没有取到对应参数的值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码