错误提示:未将对象引用设置到对象的实例(就是这一行:string news_id = Request.QueryString["news_id"].ToString();)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;public partial class manage_News_datail : System.Web.UI.Page
{ public string News_title = "";
public string News_zuozhe = "新闻作者:";
public string News_neirong = "";
public string News_AddDatetime = "录入时间:";
public string pre_News = "";
public string next_News = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string news_id = Request.QueryString["news_id"].ToString();
if (news_id.Trim() == "")
{
return;
}
DataSet ds = News_GetByID(news_id); if (ds == null && ds.Tables.Count <= 0 && ds.Tables[0].Rows.Count <= 0)
{
return;
}
AddNewsneirong(ds.Tables[0]);
AddPreNews(ds.Tables[1]);
AddNextNews(ds.Tables[2]);
} } private void AddPreNews(DataTable dt)
{
if (dt == null || dt.Rows.Count <= 0)
{
pre_News = "呵呵,没有啦!";
return;
}
DataRow dr=dt.Rows[0];
pre_News = "A href=News_datail.aspx?news_id=" + dr["id"].ToString() + ">" + dr["News_title"].ToString() + "</A>"; } private void AddNextNews(DataTable dt)
{
if (dt == null || dt.Rows.Count <= 0)
{
next_News = "呵呵,没有啦!";
return;
} DataRow dr=dt.Rows[0]; next_News = "A href=News_datail.aspx?news_id=" + dr["id"].ToString() + ">" + dr["News_title"].ToString() + "</A>";
} private void AddNewsneirong(DataTable dt)
{
if (dt == null || dt.Rows.Count <= 0)
{
return;
}
DataRow dr=dt.Rows[0]; this.News_title=dr["News_title"].ToString();
this.News_zuozhe += dr["News_zuozhe"].ToString();
this.News_neirong=dr["News_neirong"].ToString();
this.News_AddDatetime += dr["News_AddDatetime"].ToString();
} private DataSet News_GetByID(string news_id)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["xiaoxueConnectionString3"].ConnectionString;
SqlCommand command = new SqlCommand();
command.CommandText = "News_GetByID";
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
SqlParameter parms = new SqlParameter();
parms.ParameterName = "@intNewsID";
parms.SqlDbType = SqlDbType.Int;
parms.Value = news_id; command.Parameters.Add(parms); DataSet ds = new DataSet(); try
{
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
conn.Close();
}
catch
{
conn.Close();
return null;
} return ds;
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;public partial class manage_News_datail : System.Web.UI.Page
{ public string News_title = "";
public string News_zuozhe = "新闻作者:";
public string News_neirong = "";
public string News_AddDatetime = "录入时间:";
public string pre_News = "";
public string next_News = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string news_id = Request.QueryString["news_id"].ToString();
if (news_id.Trim() == "")
{
return;
}
DataSet ds = News_GetByID(news_id); if (ds == null && ds.Tables.Count <= 0 && ds.Tables[0].Rows.Count <= 0)
{
return;
}
AddNewsneirong(ds.Tables[0]);
AddPreNews(ds.Tables[1]);
AddNextNews(ds.Tables[2]);
} } private void AddPreNews(DataTable dt)
{
if (dt == null || dt.Rows.Count <= 0)
{
pre_News = "呵呵,没有啦!";
return;
}
DataRow dr=dt.Rows[0];
pre_News = "A href=News_datail.aspx?news_id=" + dr["id"].ToString() + ">" + dr["News_title"].ToString() + "</A>"; } private void AddNextNews(DataTable dt)
{
if (dt == null || dt.Rows.Count <= 0)
{
next_News = "呵呵,没有啦!";
return;
} DataRow dr=dt.Rows[0]; next_News = "A href=News_datail.aspx?news_id=" + dr["id"].ToString() + ">" + dr["News_title"].ToString() + "</A>";
} private void AddNewsneirong(DataTable dt)
{
if (dt == null || dt.Rows.Count <= 0)
{
return;
}
DataRow dr=dt.Rows[0]; this.News_title=dr["News_title"].ToString();
this.News_zuozhe += dr["News_zuozhe"].ToString();
this.News_neirong=dr["News_neirong"].ToString();
this.News_AddDatetime += dr["News_AddDatetime"].ToString();
} private DataSet News_GetByID(string news_id)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["xiaoxueConnectionString3"].ConnectionString;
SqlCommand command = new SqlCommand();
command.CommandText = "News_GetByID";
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
SqlParameter parms = new SqlParameter();
parms.ParameterName = "@intNewsID";
parms.SqlDbType = SqlDbType.Int;
parms.Value = news_id; command.Parameters.Add(parms); DataSet ds = new DataSet(); try
{
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
conn.Close();
}
catch
{
conn.Close();
return null;
} return ds;
}
}
{
string news_id = Request.QueryString["news_id"].ToString();}
if(Request.QueryString["news_id"].ToString()!="" && Request.QueryString["news_id"]!=null)
string rev= Request.QueryString["news_id"].ToString();这样就不报错了,记得给变量赋初始值
if (!IsPostBack)
{
在进行tostring()的时候,就会提示未将对象引用设置到对象的实例
你可以这样写 string news_id = "" + Request.QueryString["news_id"];
这里没有渠道值,取之前先request判断下
string id=Request.QueryString[ "news_id "]==null?"1":Request.QueryString[ "news_id "];
这样写永远不会错了