Liam Kenneth
Liam Kenneth

Reputation: 982

Test the value of url parameters then Change SqlDataSource.SelectCommand from code behind c#

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

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

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!

Here is part of my ASPX file

        <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

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?

Upvotes: 3

Views: 1207

Answers (1)

शेखर
शेखर

Reputation: 17614

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

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

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

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

Upvotes: 1

Related Questions