title: How to pass Nullable Bit type value into to SQL Server stored procedure?
body: <p>I want to pass <code>null</code> value to where clause  <code>bit</code> type variable to get all rows in a table. But also when I pass <code>true</code> or <code>false</code> where clause should work in order to that. </p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Type of conditional expression cannot be determined because
    there is no implicit conversion between 'bool?' and 'System.DBNull'</p>
  </blockquote>
  
  <p>Stored procedure:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[SelectAllUIDs] 
      @enable bit
  AS
  BEGIN
      SET NOCOUNT ON;
  
      SELECT 
          [UID], [fEnable] [Enable],
          [AddedDate] [Added Date]
      FROM 
          [VehicleService].[dbo].[NFCCard] 
      WHERE
          fEnable = @enable OR fEnable IS NOT NULL
      ORDER BY 
          NfcKy
  END
  </code></pre>
  
  <p>C# code:</p>
  
  <pre><code> public DataSet SelectUid(bool? status)
   {
          DataSet ds = new DataSet();
  
          try
          {
              SqlCommand com = new SqlCommand("SelectAllUIDs", con);
              com.CommandType = CommandType.StoredProcedure;
              com.Parameters.Add(new SqlParameter("@enable", SqlDbType.Bit)
                                     { Value = status != null ? status : DBNull.Value });
  
              SqlDataAdapter adp = new SqlDataAdapter(com);
              adp.Fill(ds);
          }
          catch (Exception ex)
          {
              ds = null;
              throw ex;
          }
          return ds;
      }
  </code></pre>
  
  <p>Calling:</p>
  
  <pre><code>DataSet ds = SelectUid(null); // should return all rows
  
  DataSet ds = SelectUid(true); // should return fEnable = true rows
  
  DataSet ds = SelectUid(false); // should return fEnable = false rows
  </code></pre>