2013-04-17 2 views
3

안녕하세요 저는 SQLDataSource로 생성 한 listView를 가지고 있는데, Sql은 URL에서 2 개의 매개 변수를 가져 와서 Select Query를 수행합니다.url 매개 변수의 값을 테스트 한 다음 SqlDataSource.SelectCommand를 코드 뒤에서 변경하십시오. C#

하지만 다음 SQL을 변경 첫번째 매개 변수의 값을 테스트 할이 문제가

경우 다른, 경우 사용 SelectCommand에 내 문은 항상 실패하고 심지어는 그들을 제거하고 페이지로드에서 쿼리를 변경할 때 경우 , 나는 selectCommand를 삭제하더라도 항상 SQLDataSource로 생성 한 원본 데이터를 반환합니다! 여기

내 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; 
     } 


    } 

내가 잘못하고있는 중이 야 어떤 생각을 파일입니다? 그것의 매개 변수 중 하나가 null의 경우 별도로 지정하지 않는 한

답변

1

의 SqlDataSource는 발생하지 않습니다 :

<asp:SqlDataSource CancelSelectOnNullParameter="False" /> 

또한 당신의 쿼리 문자열 매개 변수에 널 (null) 기본 값을 추가 할 필요가 있습니다 :

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

그냥 지금이 시도했지만 null 또는 빈 전체 텍스트 조건부 오류가 –

+0

이 실제로 작동하지 않았다 내 SQL 잘못된 :-) 감사합니다 –