How do I pass a null value into a parameter for a SqlCommand
<p>Before anyone comments that this has been answered before in other question I KNOW that..but in spite of the answers I have reviewed at</p>

<ul>
<li><a href="https://stackoverflow.com/questions/4555935/assign-null-to-a-sqlparameter">Assign null to a SqlParameter</a></li>
<li><a href="https://stackoverflow.com/questions/18963178/sending-null-parameters-to-sql-server">Sending null parameters to Sql Server</a></li>
<li><a href="https://stackoverflow.com/questions/13981281/best-method-of-assigning-null-value-to-sqlparameter">Best method of assigning NULL value to SqlParameter</a></li>
</ul>

<p>and even my own question at</p>

<ul>
<li><a href="https://stackoverflow.com/questions/36992184/sqlcommand-createparameter-value-dbnull-value-not-returning-results">SQLCommand.CreateParameter.Value = DBNull.Value not returning results</a></li>
</ul>

<p>I am unable to get my query to return values with null parameters</p>

<p>I have tried simplifying my code so it can be seen here.</p>

<p>I have also tried this with </p>

<pre><code> int? i = null;

 SqlConnection connection = new SqlConnection(Properties.Settings.Default.connstring.ToString());

 SqlCommand cmd = new SqlCommand();
 cmd.Connection = connection;
 cmd.CommandText = "Select * from view_nests where parent_pk = @parent_pk";
 cmd.Parameters.AddWithValue("@parent_pk", i ?? Convert.DBNull);

 cmd.Connection.Open();

 var dataReader = cmd.ExecuteReader();
 var dataTable = new DataTable();
 dataTable.Load(dataReader);

 cmd.Connection.Close();
</code></pre>

<p>I have tried variations on this where I just did</p>

<pre><code>cmd.Parameters.AddWithValue("@parent_pk", DBNull.Value);
</code></pre>

<p>And I have tried using the query</p>

<pre><code>cmd.CommandText = "Select * from view_nests where parent_pk = @parent_pk or @parent_pk Is Null";
</code></pre>

<p>I tried explicitly declaring the parameter as nullable</p>

<pre><code>cmd.Parameters.AddWithValue("@parent_pk", i ?? Convert.DBNull).IsNullable = true;
</code></pre>

<p>Which for some reason I thought I had working hence my reason for accepting the answer I did but I was mistaken that just returns everything to me no matter what the value.</p>

<p>I know the command object is connecting and returns data because if I put in a valid value (say 27)  it returns the record...I also know that there are records with Null as the value...but no matter how I try to set it up I keep getting nothing returns when I try to pass a null value as the parameter.</p>

<p>Anyone who can help me figure out what I'm doing wrong here I would be grateful.</p>