質問

I have a query that executes in my C# code:

protected void btnSearch_Click(object sender, EventArgs e) {
        Conn = new SqlConnection(cString);
        Conn.Open();

        theGender = slcGender.SelectedItem.Text;

        if (slcLocation.SelectedItem.Value == "") {
            locVal = slcLocation.SelectedItem.Value;
        }
        if (slcLocation.SelectedItem.Value != "") {
            locVal = slcLocation.SelectedItem.Text;
        }

        if (slcSpecialty.SelectedItem.Value == "") {
            speVal = slcSpecialty.SelectedItem.Value;
        }
        if (slcSpecialty.SelectedItem.Value != "") {
            speVal = slcSpecialty.SelectedItem.Text;
        }

        if (slcGender.SelectedItem.Value == "") {
            genVal = slcGender.SelectedItem.Value;
        }
        if (slcGender.SelectedItem.Value != "") {
            genVal = theGender.Substring(0, 1);
        }

        sqlCode = 
    "DECLARE @strLocation varchar(200)
    SET @strLocation = '" + locVal + "'

    DECLARE @strSpecialty varchar(200)
    SET @strSpecialty = '" + speVal + "'

    DECLARE @strGender varchar(200)
    SET @strGender = '" + genVal + "'

    SELECT
        [content_title] AS [Physician Name]
        , CAST([content_html] AS XML).value('(root/Physicians/picture/img/@src)[1]','varchar(255)') AS [Image]
        , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('root/Physicians/gender'))) AS [Gender]
        , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office1/a') AS [Office1]
        , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office2/a') AS [Office2]
        , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office3/a') AS [Office3]
        , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office4/a') AS [Office4]
        , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('/root/Physicians/phone1'))) AS [Phone #]
    FROM
        [database].[dbo].[content]
    WHERE
        [folder_id] = '188'
        AND
        (content_html LIKE '%<gender>'+ @strGender+'</gender>%')
        AND
        (content_html LIKE '%'+@strSpecialty+'%')
        AND
        (content_html LIKE '%'+@strLocation+'%')
    ORDER BY
        [content_title]";

        /* EXECUTE AND DISPLAY THE DATA IN THE ASP PAGE */


        using(SqlCommand command = new SqlCommand(sqlCode, Conn)) {
            command.CommandType = CommandType.Text;

            using (SqlDataReader reader = command.ExecuteReader()) {
                if (reader.HasRows) {
                    rptContent.DataSource = reader;
                    rptContent.DataBind();
                }
                else {
                    dlo.InnerHtml = "NO RESULT";
                }
            }
        }
    }

The declaration are for three separate dropdownlist in my ASP.net page. I am having an issue where If I execute the query with the above code, no result will show up, although there should be about 5 results.

The three dropdownboxes are as follow:

enter image description here

The All Locations, All Specialties, Any Gender has value of "" which should makes the following code search for ALL, if I understand correctly?

    (content_html LIKE '%<gender>'+ @strGender+'</gender>%')
    AND
    (content_html LIKE '%'+@strSpecialty+'%')
    AND
    (content_html LIKE '%'+@strLocation+'%')

The only time it works if I fill in the gender and/or location along with the specialty, before the query gives me anything back.

How do I resolve it?

役に立ちましたか?

解決

If gender is missing, it is probably missing, so there are no <gender> delimiters. Perhaps this will fix the problem:

(@strGender = '' or (content_html LIKE '%<gender>'+ @strGender+'</gender>%')) . . .
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top