locked
Using DropDownList empty value as SelectCommand ControlParameter RRS feed

  • Question

  • User-1939834628 posted

    I need to use a DropDownList as a control to provide parameters to my query.

                    <asp:ControlParameter 
                        Direction="Input" 
                        DbType="String"
                        ControlID="_ddlMonth" 
                        ConvertEmptyStringToNull="true"  
                        PropertyName="SelectedValue" 
                        DefaultValue=""
                        Name="month" />

    Such DropDownList is defined like this:

                                    <asp:DropDownList 
                                        ClientIDMode="Static"
                                        ID="_ddlMese" 
                                        runat="server" 
                                        EnableViewState="true" >
    <asp:ListItem Value="" Text="ANY" Selected ="True" /> <asp:ListItem Value="1" Text="January" /> <asp:ListItem Value="2" Text="February" /> <asp:ListItem Value="3" Text="March" /> <asp:ListItem Value="4" Text="April" /> <asp:ListItem Value="5" Text="May" /> <asp:ListItem Value="6" Text="June" /> <asp:ListItem Value="7" Text="July" /> <asp:ListItem Value="8" Text="August" /> <asp:ListItem Value="9" Text="Septemeber" /> <asp:ListItem Value="10" Text="October" /> <asp:ListItem Value="11" Text="November" /> <asp:ListItem Value="12" Text="December" /> </asp:DropDownList>

    The value ANY should be used to bypass month check in my query as described below.

    My SelectCommand includes an AND section in WHERE clause:

    AND ((@month IS NULL) OR (table.month = @month))

    I thought ANY value (empty string) should be converted to NULL at the time it becomes a parameter value. 

    The query works fine if I choose a real month value (from DDL) but if I select ANY, even if I specified ConvertEmptyStringToNull="true" option in ControlParameter, @month parameter never get NULL and the query probably ends up checking somehting like this:

    '' IS NULL

    that's never true and then returns no records.

    There's a way to fix this?

    Friday, December 18, 2020 10:01 AM

All replies

  • User475983607 posted

    Check for an empty string rather than null.

    (@month = '')

    Friday, December 18, 2020 11:01 AM
  • User-1939834628 posted

    Quite simple I'm going to try it... I would like also know if ConvertEmptyStringToNull option works or not.

    Friday, December 18, 2020 11:09 AM
  • User475983607 posted

    Quite simple I'm going to try it... I would like also know if ConvertEmptyStringToNull option works or not.

    No.  The DropDownList shown above is hard coded.

    Friday, December 18, 2020 11:13 AM
  • User-1939834628 posted

    weirdgyn1972

    Quite simple I'm going to try it... I would like also know if ConvertEmptyStringToNull option works or not.

    No.  The DropDownList shown above is hard coded.

    i.t. looks like putting CancelSelectOnNullParameter="false"  in SQLDataSource it works

    Friday, December 18, 2020 11:16 AM