2011-08-12 61 views
-2

每當我直接進入我的頁面(mypage.aspx)它返回一個錯誤:(?mypage.aspx SR =真)如果我添加一個查詢字符串查詢字符串驗證問題

Object not set to object not set to an instance of an object.

它的工作原理,但我檢查,確保在它評估它有一個值之前,它不應該有一個值。那麼,爲什麼我直接訪問頁面時會出現錯誤?

if (!IsPostBack) 
{ 
    string qu1 = ""; 
    string qu2 = ""; 
    string qu3 = ""; 

    if (Request.QueryString["qu1"] != null) 
    { 
     qu1 = Request.QueryString["qu1"].ToString(); 
     if (qu1 != "") 
     { 
      qu1DropDownList.SelectedValue = industry; 
     } 
    } 

    if (Request.QueryString["qu2"] != null) 
    { 
     qu2 = Request.QueryString["qu2"].ToString(); 
     if (qu2 != "") 
     { 
      qu2DropDownList.SelectedValue = category; 
     } 
    } 

    fillDropDownList(); 

    if (Request.QueryString["qu3"] != null) 
    { 
     qu3 = Request.QueryString["qu3"].ToString(); 
     if (qu3 != "") 
     { 
      qu3tDropDownList.SelectedValue = product; 
     } 
    } 
} 

string search = ""; 
string qu1value = IndustryDropDownList.SelectedValue; 
string qu2value = ProductCategoryDropDownList.SelectedValue; 
string qu3value = ProductDropDownList.SelectedValue; 

using (SPSite site = new SPSite("SITE")) 
using (SPWeb oWebsiteRoot = site.OpenWeb()) 
{ 
    SPList oList = oWebsiteRoot.Lists["SpacesInfo"]; 
    SPListItemCollection items = null; 

    if (Request.QueryString["sr"] != "" && Request.QueryString["sr"] != null) 
    { 
     search = Request.QueryString["sr"].ToString(); 
     if (search == "true") 
     { 
      if (indvalue == "select" & catvalue == "select") 
      { 
       items = oList.Items; 
      } 
      else if (indvalue != "select" & catvalue != "select" & provalue != "select") 
      { 
       SPQuery query = new SPQuery(); 
       query.Query = "MYQUERY"; 
       items = oList.GetItems(query); 
      } 
      else if (indvalue != "select" & catvalue != "select" & provalue == "select") 
      { 
       SPQuery query = new SPQuery(); 
       query.Query = "MYQUERY"; 
       items = oList.GetItems(query); 

      } 
      else if (indvalue != "select" & catvalue == "select") 
      { 
       SPQuery query = new SPQuery(); 
       query.Query = "<Where><Eq><FieldRef Name='Industry' /><Value Type='Choice'>" + indvalue + 
        "</Value></Eq></Where>"; 
       items = oList.GetItems(query); 
      } 
      else if (indvalue == "select" & catvalue != "select" & provalue == "select") 
      { 
       SPQuery query = new SPQuery(); 
       query.Query = "MYQUERY"; 
       items = oList.GetItems(query); 
      } 
      else if (indvalue == "select" & catvalue != "select" & provalue != "select") 
      { 
       SPQuery query = new SPQuery(); 
       query.Query = "MYQUERY"; 
       items = oList.GetItems(query); 
      } 
      else 
      { 
       errorLabel.Text = "Please contact the administrator."; 
       items = oList.Items; 
      } 
     } 
     else 
     { 
      items = oList.Items; 
     } 
    } 
    DataTable table = new System.Data.DataTable(); 
    table = items.GetDataTable(); 
    spacerepeater.DataSource = table; 
    spacerepeater.DataBind(); 
} 
+4

凡究竟異常發生的?什麼線? –

回答

0

改變,如果語句的順序是:

if (Request.QueryString["sr"] != "" && Request.QueryString["sr"] != null) 

應該

if (Request.QueryString["sr"] != null && Request.QueryString["sr"] != "") 

的問題是這樣的,在你的代碼看起來爲空字符串( 「」)和因對象爲空而失敗。在我的版本中,它會看到該值爲空,並且無法檢查第二個if語句。

這通常被稱爲短路評估。

裁判http://en.wikipedia.org/wiki/Short-circuit_evaluation

+1

或者只是使用'if(!String.IsNullOrEmpty(Request.QueryString [「sr」]))' –

+3

這個命令會如何導致一個問題?比較'null'到一個空字符串將工作得很好 – BrokenGlass

+0

@BrokenGlass由OP報告它會拋出一個錯誤(它是一個左值)。親自嘗試一下。 – Hogan

0

用途:

if (!String.IsNullOrEmpty(Request.QueryString["sr"])) 
{ 
    //Do stuff 
}