2012-08-11 160 views
0

我有一個問題讓我堆疊,我無法找到任何解決方案,非常需要你的幫助:其實我需要在員工表上進行一些搜索,就像我需要搜索所有女性或已經得到phd等職位,我做了這個查詢僱員表的函數,如果條件和這個搜索的結果進入一個gridview並允許爲真的問題是,只要我點擊下一步去到gridview中的其他頁面,它做了一個完整的回發,並再次綁定到gridview數據源,因爲我給gridview數據源下面的函數(DetailedSearch)與if cluases當它進入函數時,它開始閱讀與第一條件如果沒有去該特定條件,然後拋出異常未將對象引用設置爲對象的實例Gridview分頁問題

這裏是我的代碼(這是它只是一個部分不完整的代碼):

public DataTable DetailedSearch() 
    { 
     con.Open(); 
     SqlDataAdapter da=new SqlDataAdapter(); 


     if (FirstName != string.Empty) 
     { 
      if (FirstName != string.Empty && LastName != "--Letters--") 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'"+"and lastname like '"+LastName.ToString()+"%'", con); 
      } 
      else if(FirstName!=string.Empty && Gender!="{Please Select}") 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and gender='" + Gender.ToString() + "'", con); 

      } 
      else if(FirstName!=string.Empty && MaritalStatus!="{Please Select}") 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and maritalstatus='" + MaritalStatus.ToString() + "'", con); 
      } 
      else if(FirstName!=string.Empty && Qualification!="{Please Select}") 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and qualification='" + Qualification.ToString() + "'", con); 
      } 
      else if(FirstName!=string.Empty && GraduationDate!=DateTime.MinValue) 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and graduationdate='" + GraduationDate.ToString() + "'", con); 
      } 
      else if(FirstName!=string.Empty && Province!="{Please Select}") 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and province='" + Province.ToString() + "'", con); 
      } 
      else if(FirstName!=string.Empty && LastEmployer!=string.Empty) 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and lastemployer='" + LastEmployer.ToString() + "'", con); 
      } 
      else if(FirstName!=string.Empty && EnteredBy!="{Please Select}") 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and enteredby='" + EnteredBy.ToString() + "'", con); 
      } 
      else if (FirstName != string.Empty && DateEntered != DateTime.MinValue) 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'" + "and dateentered='" + DateEntered.ToString() + "'", con); 
      } 
      else 
      { 
       da = new SqlDataAdapter("select * from employee where firstname ='" + FirstName.ToString() + "'", con); 
      } 
     } 
     else if (LastName != "--Letters--") 
     { 
       if (LastName != "--Letters--" && Province != "{Please Select}") 
       { 
        da = new SqlDataAdapter("select * from employee where lastname like'" + LastName.ToString() + "%'" + "and province='" + Province.ToString() + "'", con); 
       } 
       else if (Gender != "{Please Select}" && LastName != "--Letters--") 
       { 
        da = new SqlDataAdapter("select * from employee where gender='" + Gender.ToString() + "'" + "and lastname like '" + LastName.ToString() + "%'", con); 
       } 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     con.Close(); 
     return dt; 

    } 

請幫我該怎麼辦,是完全以空白,沒有邏輯來了我的腦海裏,我必須把if引導的從句的查詢可能是不一樣的基礎上,搜索,但如何讓gridview的分頁意識到了這一點:(提前

感謝

+0

在什麼行引發異常? – Andre 2012-08-11 03:38:22

回答

0

真的,也有可以改善的這麼多東西在那個代碼...

  1. 你在做FirstName != string.Empty反覆,但你已經 在第一次測試它。
  2. 你應該改變以單一if(!String.IsNullOrEmpty(FirstName))
  3. 您應該使用sql parameters而不是字符串連接,因爲這避免了潛在的SQL注入
  4. 你不應該傳遞字段的默認用戶界面,像「請選擇」 。在選擇默認值情況下 ,傳遞空或空值

雖然這不回答或具體問題,這是一個很好的指導 - 事實上,在情況下,你嘗試應用這些變化,它甚至可以解決你的問題在這個過程中