测试url参数的值然后从c#后面的代码更改SqlDataSource.SelectCommand

时间:2023-01-13 10:31:05

Hi I have a listView that I generated with a SQLDataSource, The Sql gets 2 parameters from the URL then performs a Select Query.

您好我有一个我用SQLDataSource生成的listView,Sql从URL获取2个参数然后执行选择查询。

But I want to test the value of the parameters 1st then change the sql SelectCommand using If, else If

但我想测试参数的值1st然后使用If更改sql SelectCommand,否则如果

The Problem is my IF statements always fail and even when I remove them and change the query on page load, I always get returned the original data that I generated with the SQLDataSource even with the selectCommand deleted!

问题是我的IF语句总是失败,即使我删除它们并在页面加载时更改查询,我总是返回我使用SQLDataSource生成的原始数据,即使删除了selectCommand!

Here is part of my ASPX file

这是我的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>

Here is my .CS File

这是我的.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;
        }


    }

Any idea what I am doing wrong?

知道我做错了什么吗?

1 个解决方案

#1


1  

SqlDataSource won't fire if any of it's parameters are null, unless you specify otherwise:

除非您另行指定,否则如果SqlDataSource的任何参数为null,则不会触发它:

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

It might also be necessary to add a null default value to your querystring parameter:

可能还需要在querystring参数中添加一个null默认值:

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

#1


1  

SqlDataSource won't fire if any of it's parameters are null, unless you specify otherwise:

除非您另行指定,否则如果SqlDataSource的任何参数为null,则不会触发它:

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

It might also be necessary to add a null default value to your querystring parameter:

可能还需要在querystring参数中添加一个null默认值:

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