我的页面里面有一个iframe页面,现在要求如果不点击连接默认iframe页面是到数据库中提取一条指定的记录显示其内容。如果点击相应连接就到数据库中提取相应记录显示。具体代码如下:public class cont : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label xxnews;
protected System.Web.UI.WebControls.Label xxtitle;
public DataView dv;
string a="";
string b="";
private void Page_Load(object sender, System.EventArgs e)
{
a=Request.QueryString["class1"].ToString().Trim();
b=Request.QueryString["class2"].ToString().Trim();
if (a=="") {a="集团概况";}
if (b=="") {b="集团简介";}
SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
string sqlstring = "SELECT * FROM jq_news where class1='"+a+"' and class2='"+b+"'";
SqlDataAdapter MyCommand = new SqlDataAdapter(sqlstring, MyConnection);
DataSet ds = new DataSet();
MyCommand.Fill(ds, "jq_news");
dv = ds.Tables["jq_news"].DefaultView;
if (!IsPostBack)
{
this.xxtitle.Text=ds.Tables["jq_news"].Rows[0]["jqtitle"].ToString();
this.xxnews.Text=ds.Tables["jq_news"].Rows[0]["jqnews"].ToString();
}
MyConnection.Close();
}
我这样写报错:未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 26: private void Page_Load(object sender, System.EventArgs e)
行 27: {
行 28: a=Request.QueryString["class1"].ToString().Trim();
行 29: b=Request.QueryString["class2"].ToString().Trim();
行 30: if (a=="") {a="集团概况";} 源文件: d:\jinqiao\cont.aspx.cs 行: 28
{
protected System.Web.UI.WebControls.Label xxnews;
protected System.Web.UI.WebControls.Label xxtitle;
public DataView dv;
string a="";
string b="";
private void Page_Load(object sender, System.EventArgs e)
{
a=Request.QueryString["class1"].ToString().Trim();
b=Request.QueryString["class2"].ToString().Trim();
if (a=="") {a="集团概况";}
if (b=="") {b="集团简介";}
SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
string sqlstring = "SELECT * FROM jq_news where class1='"+a+"' and class2='"+b+"'";
SqlDataAdapter MyCommand = new SqlDataAdapter(sqlstring, MyConnection);
DataSet ds = new DataSet();
MyCommand.Fill(ds, "jq_news");
dv = ds.Tables["jq_news"].DefaultView;
if (!IsPostBack)
{
this.xxtitle.Text=ds.Tables["jq_news"].Rows[0]["jqtitle"].ToString();
this.xxnews.Text=ds.Tables["jq_news"].Rows[0]["jqnews"].ToString();
}
MyConnection.Close();
}
我这样写报错:未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 26: private void Page_Load(object sender, System.EventArgs e)
行 27: {
行 28: a=Request.QueryString["class1"].ToString().Trim();
行 29: b=Request.QueryString["class2"].ToString().Trim();
行 30: if (a=="") {a="集团概况";} 源文件: d:\jinqiao\cont.aspx.cs 行: 28
if (Request.QueryString["class1"] == null)
{
a="集团概况";
}
else
{
a=Request.QueryString["class1"].ToString().Trim();
}
b同a类似
如果是未点击该页面的时候那么Request.QueryString["class1"]的值为null
你在a=Request.QueryString["class1"].ToString().Trim(); 中的操作就等同于
a = null.ToString().Trim();
所以出现未将对象引用设置到对象的实例的错误