如果值为空,为什么查询不起作用

时间:2014-05-15 21:16:04

标签: asp.net sql

我有一个在我的C#代码中执行的查询:

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

该声明适用于我的ASP.net页面中的三个单独的下拉列表。我遇到了一个问题,如果我使用上面的代码执行查询,则不会显示任何结果,尽管应该有大约5个结果。

三个下拉框如下:

enter image description here

All LocationsAll SpecialtiesAny Gender的值为"",如果我理解正确,应该使以下代码搜索ALL?

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

如果我在查询给我回复之前填写性别和/或位置以及专业,那么它唯一有效的时间。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

如果缺少性别,则可能缺少性别,因此没有<gender>分隔符。也许这将解决问题:

(@strGender = '' or (content_html LIKE '%<gender>'+ @strGender+'</gender>%')) . . .