I have the following code as part of an sqlCommand
which is calling a stored procedure:
Dim groupObj
If groupId = 0 Then
groupObj = DBNull.Value
Else
groupObj = groupId
End If
Dim siteObj
If siteId = 0 Then
siteObj = DBNull.Value
Else
siteObj = siteId
End If
sqlCommand.Parameters.Add(New SqlClient.SqlParameter("GroupID", SqlDbType.Decimal)).Value = groupObj
sqlCommand.Parameters.Add(New SqlClient.SqlParameter("SiteID", SqlDbType.Decimal)).Value = siteObj
I basically want to check for a condition and (if true
) send a DBNull
instead. This code looks awful however - is there a way to succinct this up?
null
/Nothing
value and automatically translating that into a database null value. DBNull generally comes into play when you are reading data out of a database, rather than writing to it. \$\endgroup\$??
operator would not be applicable in this specific situation. Finally,??
is the C# coalesce operator; the VB.NET equivalent would be to call theIf
function with only 2 arguments. \$\endgroup\$