I was previously taught today how to set parameters in a SQL query in .NET in this answer (click).
Using parameters with values are fine, but when I try to set a field in the database to null I’m unsuccessful. Either the method thinks I am not setting a valid parameter or not specifying a parameter.
e.g.
Dim dc As New SqlCommand('UPDATE Activities SET [Limit] = @Limit WHERE [Activity] = @Activity', cn) If actLimit.ToLower() = 'unlimited' Then ' It's not nulling :( dc.Parameters.Add(New SqlParameter('Limit', Nothing)) Else dc.Parameters.Add(New SqlParameter('Limit', ProtectAgainstXSS(actLimit))) End If
Is there something I’m missing? Am I doing it wrong?
you want DBNull.Value.
In my shared DAL code, I use a helper method that just does: