Reputation: 372
i have xml file, and in this file is for example this:
<VARIANT2></VARIANT2>
I loaded data from xml and after that i write this data into sql. This is command to add value from VARIANT2 to sql database:
cmd.Parameters.AddWithValue("@var2", item.VARIANT2);
But if the VARIANT2 is emty than into sql table has writen 'blank sign'. It is possible, when is VARIANT2 empty write nothing to sql database? I want had in column NULL value..
Upvotes: 0
Views: 355
Reputation: 35891
As the documentation suggests, try this:
object value
= string.IsNullOrEmpty(item.VARIANT2) ? (object)DBNull.Value : item.VARIANT2;
cmd.Parameters.AddWithValue("@var2", value);
Upvotes: 3
Reputation: 63065
if it is null you need to insert it as DBNull.Value
cmd.Parameters.AddWithValue("@var2", string.IsNullOrEmpty(item.VARIANT2) ?
(object)DBNull.Value : item.VARIANT2);
Upvotes: 3