Search code examples
asp.netsqlsql-servervisual-studio-2012sqldatasource

Pass is NULL instead of = Null


This is my sqldatasource

   <asp:SqlDataSource runat="server" ID="get_client_users" ConnectionString="<%$ ConnectionStrings:local %>" SelectCommand="SELECT *  FROM users WHERE clientid = @clientid" CancelSelectOnNullParameter="False">
    <SelectParameters>
        <asp:Parameter Name="clientid" Type="Int32"  ConvertEmptyStringToNull="true"/>
    </SelectParameters>
   </asp:SqlDataSource>

Now I want is that when clientid is null then it should return all the rows with clientid=null, but when I watch the query passed in profiler i get this.

exec sp_executesql N'SELECT *  FROM users WHERE clientid = @clientid',N'@clientid int',@clientid=NULL

but it will only return values if its

clientid is NULL

As I have ConvertEmptyStringToNull="true" command is executing, but returning empty dataset.


Solution

  • UPDATE:

    SELECT *
    FROM users 
    WHERE ISNULL(clientID, -1) = ISNULL(@ClientID, -1)
    

    This is assuming that clientID will never be -1 in the database.