C# 对象引用未设置为对象的实例。请求.查询字符串
我知道有人问过这个问题,但我的情况是: 正确的url是:C# 对象引用未设置为对象的实例。请求.查询字符串,c#,asp.net,string,C#,Asp.net,String,我知道有人问过这个问题,但我的情况是: 正确的url是:http://localhost:1478/application/ProductID.aspx?ID=1001 这将检索我的产品列表。但是,如果我删除查询字符串,就说: http://localhost:1478/application/ProductID.aspx 错误是对象引用未设置为对象的实例。在这个问题中有错误处理的实现吗?基本上你会得到这个错误,因为页面需要缺少的信息来完成它的过程 如果希望它在没有错误的情况下运行,则需要一
http://localhost:1478/application/ProductID.aspx?ID=1001
这将检索我的产品列表。但是,如果我删除查询字符串,就说:
http://localhost:1478/application/ProductID.aspx
错误是对象引用未设置为对象的实例。在这个问题中有错误处理的实现吗?基本上你会得到这个错误,因为页面需要缺少的信息来完成它的过程 如果希望它在没有错误的情况下运行,则需要一个“If”块来检查(产品)Id参数是否已在querystring或url中传递 VB 改变
string ProductID = Request.QueryString["ID"].ToString();
string ProductID = Request.QueryString["ID"].ToString();
到
改变
string ProductID = Request.QueryString["ID"].ToString();
string ProductID = Request.QueryString["ID"].ToString();
到
尝试这种方法,首先检查查询字符串返回的条件NOTNULL。请参阅下面的代码
int ProductID ;
if(Request.QueryString["ID"]!=null)
{
ProductID =Convert.ToInt32( Request.QueryString["ID"].ToString());
showResult.YourmethodName(ProductID);
}
else
{
//Show one error lik "INVALID URL" ,Please go back
}
您能否发布导致问题的代码周围的行。它将帮助我们找出如何更好地帮助您。第68行:private void BindGridMain()第69行:{第70行:string ProductID=Request.QueryString[“ID”].ToString();//问题在这里第71行:ShowProductSearchResult showResult=new ShowProductSearchResult();第72行:DataList1.DataSource=showResult.ShowProductResult(ProductID);不看代码,我不能确定..但我认为您使用的是类似Request.QueryString[“ID”]的东西,它将引发该异常,因为查询字符串中没有ID。我想您可以使用Request.QueryString.HasKey(“ID”)…如果我没记错的话..几乎所有的
NullReferenceException
案例都是一样的。请参阅“”以获得一些提示。另外,我已经编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。
if(!string.IsNullOrWhiteSpace(Request.QueryString["ID"]))
{
string ProductID = Request.QueryString["ID"];
DataList1.DataSource = showResult.ShowProductResult(ProductID);
}
else
{
//Do something when Id is empty or null
//DataList1.DataSource =null;
}
int ProductID ;
if(Request.QueryString["ID"]!=null)
{
ProductID =Convert.ToInt32( Request.QueryString["ID"].ToString());
showResult.YourmethodName(ProductID);
}
else
{
//Show one error lik "INVALID URL" ,Please go back
}