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.

有帮助吗?

解决方案

UPDATE:

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

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

其他提示

Since you have complete control over the SELECT command, I'd have thought:

SELECT *  FROM users
WHERE clientid = @clientid OR
      (@clientid is null and clientid is null)

Would work for you.

You can try,

@ClientID IS NULL OR ClientID=@ClientID

This should work

SELECT *
FROM users 
WHERE clientID = ISNULL(@ClientID, clientID)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top