public partial class message : System.Web.UI.Page
{
    string postid;
    protected void Page_Load(object sender, EventArgs e)
    {
        postid = Request["postid"].ToString();  这里提示:用户代码未处理:NullReferenceException,未将对象引用设置到对象的实例。
        Update_Views();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("relpy.aspx?postid=" + postid);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("newpost.aspx");
    }
    protected void ExcuteSql(string strSql)
    {
        string strconn = "Server=(local);Database=aspnet_test;Integrated Security=true";
        DbProviderFactory dbproviderfactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
        DbConnection dbconn = dbproviderfactory.CreateConnection();
        dbconn.ConnectionString = strconn;
        dbconn.Open();
        DbCommand dbcomm = dbproviderfactory.CreateCommand();
        dbcomm.Connection = dbconn;
        dbcomm.CommandText = strSql;
        dbcomm.ExecuteNonQuery();
        dbconn.Close();
        dbconn.Dispose();
    }
    protected void Update_Views()
    {
        string strSql = "Update newpost set views=views+1 where postid=" + postid;
        ExcuteSql(strSql);
    }
}

解决方案 »

  1.   

    postid没有这个参数值.

     postid = Request["postid"];
    if(postid!=null)
    {
           Update_Views(); 
    }
      

  2.   

    那是因为postid这个值没有传递过来啊,你的URL是什么
      

  3.   

    Request["postid"]应该为null,做一下判断
    if( Request["postid"]!=null){
     postid = Request["postid"].ToString();  
    }
      

  4.   

     postid 赋个初始值,或者是判断 postid 是否为空
      

  5.   

    是的,像一楼这样做就OK你在引用一个对象时,如果对象是null的,然后你再加一个tostring(),是肯定报错的了