You need to make sure that CancelSelectOnNullParameter is set to false on your SqlDataSource, and that ConvertEmptyStringToNull is true for the required parameter. In the markup, it should look something like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" CancelSelectOnNullParameter="false" SelectCommand="..."> <SelectParameters> <asp:ControlParameter Name="..." ControlID="..." PropertyName="..." DbType="..." ConvertEmptyStringToNull="true"/> ... </SelectParameters> </asp:SqlDataSource>
The problem will start if you have more than one control that can provide a null value, and you want to allow only one of them to be null. In this case, you should set CancelSelectOnNullParameter to true and use the Selecting event to add DBNull.Value:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { if (String.IsNullOrEmpty(textBox.Text)) ((IDbDataParameter)e.Command.Parameters["@name"]).Value = DBNull.Value; }
This should allow you to solve your problems.
source share