要疯了,关于Request.QueryString取值提示
未将对象引用设置到对象的实例。
这是个展示图片的列表页,当url为pics.aspx?classid=3这样时页面正常显示,如果是pics.aspx则提示:未将对象引用设置到对象的实例。
下面是我的代码:
public partial class pics : System.Web.UI.Page
{
protected string cid=string .Empty ;
protected string tit;
protected string key = string.Empty;
protected string sql = string.Empty;
private void bdpics()
{
dal dal = new dal();
if (key == "")
{
sql = "select id,title,classid,DefaultPicUrl from WygkcnZWB_Product where classid=? order by id desc";
OleDbParameter[] pas ={
new OleDbParameter ("@cid",cid)};
lvpic.DataSource = dal.getdataset(sql, "pics",pas );
}
else
{
sql = "select id,title,classid,DefaultPicUrl from WygkcnZWB_Product where classid=? and title like '%'+?+'%' order by id desc";
OleDbParameter[] pas = {
new OleDbParameter ("@cid",cid),
new OleDbParameter ("@key",key)};
lvpic.DataSource = dal.getdataset(sql, "pics",pas);
}
lvpic.DataBind();
uc_top.tkey = "";
}
protected void Page_PreRender(object sender, EventArgs e)
{
bdpics();//listview数据绑定必须放在这里,否则得不到正确结果
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (uc_top.tkey=="")
{
key = "";
}
else
{
key = uc_top.tkey;
}
if (Request.QueryString["classid"].ToString() != "" || Request .QueryString ["classid"].Equals (null)) //这里提示错误
{
cid = Request.QueryString["classid"].ToString();
}
switch (cid)
{
case "1":
tit = "企业产品";
break;
case "3":
tit = "工程业绩";
break;
default:
tit = "企业产品";
break;
}
}
}
}
if (Request.QueryString["classid"].ToString() != "" || Request .QueryString ["classid"].Equals (null))
提示这句错了,要怎么改才行?
------解决方案--------------------先判断是否为null:
if(Request.QueryString["classid"]!=null)
if(你的代码)
------解决方案--------------------Request.QueryString["classid"].ToString() != ""
一个null的对象直接tostring,当然报错了。
你需要先判断 Request.QueryString["classid"]是否为Null
------解决方案--------------------C# code
string classID = Request.QueryString["classid"]
if(!string.IsNullOrEmpty(classID))
{
cid=classID;
//TODO: anything
}
----