2013-04-17 61 views
3

嗨我有一個listView,我用SQLDataSource生成,Sql從URL獲取2個參數,然後執行Select Query。測試url參數的值,然後從代碼後面更改SqlDataSource.SelectCommand#

但我要測試的參數,第一個,然後更改SQL值的SelectCommand使用如果,否則,如果

問題是我的IF語句總是失敗,甚至當我刪除和更改在頁面加載查詢,我總是會返回使用SQLDataSource生成的原始數據,即使刪除了selectCommand也是如此!

這裏是我的ASPX文件

 <asp:SqlDataSource ID="jobSearch" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand=""> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="jobTitle" QueryStringField="jobTitle" Type="String" /> 
      <asp:QueryStringParameter Name="joblocation" QueryStringField="jobLocation" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 

這裏的一部分,我的cs文件

protected void Page_Load(object sender, EventArgs e) 
    { 
     // Request.QueryString["jobTitle"] 

     string jobTitle = Request.QueryString["jobTitle"]; 
     string jobLocation = Request.QueryString["jobLocation"]; 

     if (!string.IsNullOrEmpty(jobTitle) && !string.IsNullOrEmpty(jobLocation)) 
     { 
      jobSearch.SelectCommand = "SELECT [jobId], [userId], [jobTitle], [jobBody], [jobPosition], [JobType], [salaryFrom], [salaryTo], [salaryType], [location], [jobCreated], [jobEnd], [jobViews], [applications] FROM [recruiter_Jobs] WHERE FREETEXT (([jobTitle]), @jobTitle) AND FREETEXT (([location]), @location) ORDER BY [jobCreated]"; 
      test.Text = "1st if " + jobTitle; 
     } 
     else if (jobTitle == string.Empty && !string.IsNullOrEmpty(jobLocation) || string.IsNullOrWhiteSpace(jobTitle) && !string.IsNullOrEmpty(jobLocation) || string.IsNullOrEmpty(jobTitle) && !string.IsNullOrEmpty(jobLocation) || jobTitle == null && !string.IsNullOrEmpty(jobLocation)) 
     { 

      jobSearch.SelectCommand = "SELECT [jobId], [userId], [jobTitle], [jobBody], [jobPosition], [JobType], [salaryFrom], [salaryTo], [salaryType], [location], [jobCreated], [jobEnd], [jobViews], [applications] FROM [recruiter_Jobs] WHERE FREETEXT (([location]), @location) ORDER BY [jobCreated]"; 

      test.Text = "1st else if " + jobTitle; 
     } 

     else if (string.IsNullOrEmpty(jobTitle) && string.IsNullOrEmpty(jobLocation)) 
     { 
      jobSearch.SelectCommand = "SELECT [jobId], [userId], [jobTitle], [jobBody], [jobPosition], [JobType], [salaryFrom], [salaryTo], [salaryType], [location], [jobCreated], [jobEnd], [jobViews], [applications] FROM [recruiter_Jobs] ORDER BY [jobCreated]"; 

      test.Text = "last else if " + jobTitle; 
     } 


    } 

任何想法,我做錯了什麼?

回答

1

的SqlDataSource將不會觸發,如果任何它的參數爲null,除非另行指定:

<asp:SqlDataSource CancelSelectOnNullParameter="False" /> 

它也需要一個空默認值添加到您的查詢參數:

<asp:QueryStringParameter Name="client" QueryStringField="client" 
    DefaultValue="" ConvertEmptyStringToNull="True" /> 
+0

剛剛嘗試過,但我得到一個空或全空文本謂詞錯誤 –

+0

這確實工作,這是我的SQL是錯的:-)謝謝 –