Search code examples
asp.netgridviewsqldatasource

Passing null in textboxes not working


Hi I m trying to pass null values in my date textboxes Textbox 3 and Textbox 4 but am not able to do so. Please let me know what I am doing wrong. Basically what I need is when the page load and the textboxes are empty I need Gridview to load the entire table . at present it only loads when I enter a date range but if I leave the date textboxes empty I get an empty page.I have an Ajax calendar extension for a drop down calendar control also attached to these two text boxes. Don't know if that is the problem. Please help..

Here is the code

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:IngestConnectionString %>"
    SelectCommand="SELECT ID, Story_number, Date, Memory_card, Story_Name FROM Library WHERE (Story_Name LIKE '%' + @Story_Name + '%') AND (Story_number LIKE '%' + @Story_number + '%') AND (@startdate IS NULL OR @startdate = '' OR Date >= @startdate) AND (@enddate IS NULL OR @enddate = '' OR Date <= @enddate)">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" 
            Name="Story_Name"
            PropertyName="Text" 
            DefaultValue="%" />
        <asp:ControlParameter ControlID="TextBox2" 
            DefaultValue="%" 
            Name="Story_number"
            PropertyName="Text" />
        <%--<asp:ControlParameter ControlID="DropDownList1" 
                DefaultValue="%" 
                Name="Memory_card"
                PropertyName="SelectedValue" />--%>
        <asp:ControlParameter ControlID="TextBox3" 
            Name="startdate" 
            ConvertEmptyStringToNull="true"
            PropertyName="Text" />
        <asp:ControlParameter ControlID="TextBox4" 
            Name="enddate" 
            ConvertEmptyStringToNull="true" 
            PropertyName="Text" />
    </SelectParameters>
</asp:SqlDataSource>

It works when I put some ridiculous date range in the default values as under but I am not able to get it to pass null to the database so that I see the full table when the page loads

<asp:ControlParameter ControlID="TextBox3" 
    Name="startdate" 
    ConvertEmptyStringToNull="true" 
    DefaultValue="1/1/1977"
    PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox4" 
    Name="enddate" 
    ConvertEmptyStringToNull="true" 
    DefaultValue="1/1/2100"
    PropertyName="Text" />

Update:

Where do I add this code? I am adding it in the control parameter like this

<asp:ControlParameter ControlID="TextBox4" 
    Name="enddate" 
    SqlDataSource2.CancelSelectOnNullParameter="False" 
    PropertyName="Text" />

It gives me the following error when I do it

Literal content ('<asp:ControlParameter ControlID="TextBox4" Name="enddate" SqlDataSource2.CancelSelectOnNullParameter="False" PropertyName="Text" />') is not allowed within a 'System.Web.UI.WebControls.ParameterCollection'


Solution

  • CancelSelectOnNullParameter is a property of the SQLdatasource control, not the parameters contained in the control:

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        CancelSelectOnNullParameter="False"
        ConnectionString="<%$ ConnectionStrings:IngestConnectionString %>"
        SelectCommand="SELECT ID, Story_number, Date, Memory_card, Story_Name FROM Library WHERE (Story_Name LIKE '%' + @Story_Name + '%') AND (Story_number LIKE '%' + @Story_number + '%') AND (@startdate IS NULL OR @startdate = '' OR Date >= @startdate) AND (@enddate IS NULL OR @enddate = '' OR Date <= @enddate)">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" ...