I wanted to learn which usage is true?
if(!string.IsNullOrEmpty(parentID))
cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID));
else
cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value));
OR
if(!string.IsNullOrEmpty(parentID))
cmd.Parameters.Add(new SqlParameter("@ParentSesID", parentID));
else
cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value.ToString()));
This one:
cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value));
is a correct, in case the field you are writing to can acceptNULL
value in database.This one:
cmd.Parameters.Add(new SqlParameter("@ParentSesID", DBNull.Value.ToString()));
if the field is a, say,VARCHAR
, and does not acceptNULL
, but you want to indicate in some way an absence of value in that field, so you write inside your application specific "no value" value. So your application knows, if it founds that value in the field, that means: no value.DBNull.Value.ToString()
will return empty string, so I thinkis a good approach and here the only approach
see here:
http://ideone.com/iGo1Jh
This is passing a parentID to parameter @ParentSesID.
is passing a
null
value to parameter.is passing equal to
string.Empty
, which is not allowed in numerical data types.is same as ignoring the parameter.
So when you need to pass
null
to SP you've to passDBNull.Value
.