Kate
Kate

Reputation: 372

Load null data from xml and write null to sql

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

Answers (2)

BartoszKP
BartoszKP

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

Damith
Damith

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

Related Questions