0

Suppose you have a SQLDataSource that looks like this:

<asp:SqlDataSource ID="sqldsSample" runat="server" 
    ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
    SelectCommand="SELECT [col1], [col2] FROM [tbl] WHERE [col3] = @val) ORDER BY [col1] DESC;">
    <SelectParameters>
        <asp:Parameter DefaultValue="False" Name="val" Type="Boolean" />
        <asp:Parameter DefaultValue="" Name="val2" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

Just assume you have decided you want to declare the 'val2' parameter here and you don't want to bother with adding and/or removing parameters later in the code-behind (say, to change the SelectCommand to do some filtering with some extra criteria).

It will fail without an error - the control will just show up empty.

2 Answers 2

1

You have to specify a default value for the parameter even if it's not used. For example, just putting in a space character will work:

<asp:Parameter DefaultValue=" " Name="val2" Type="String" />

Note that it still won't work if you omit the DefaultValue property.

Sign up to request clarification or add additional context in comments.

Comments

0

There is also the CancelSelectOnNullParameter parameter on SqlDataSource - you need to set this to false if you expect any of your parameters to be null.

It's not very obvious and it has caught me out a few times!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.