Int to Null ? Is possible ?

  • Thread starter Thread starter Paperback Writer
  • Start date Start date
P

Paperback Writer

Hi All, How are you ?
I'm trying to insert null values in my database, but these datas are
datatype Int in my C# and it's converting my null values to 0 (zero)!
How could i threat that ?
 
Wait, you say you are inserting C# int datatypes into your database right?
Nothign is converting your null values to 0... a C# int cannot be null. How
do you know if a value is null or not.

System.DBNull.Value might be what you are looking for though.
 
Hey,

Since int is a valuetype you can't treat it as null.

Deal it in you buisiness logic tier (Client, DB etc.) to treat 0 as null
where necassery.

Why do you treat 0 as null? null is not so good option instead of 0.

- Moty -
 
hi,

What is your int equivalent to null , I will assume it's 0

com.Parameters.Add("@forwarder", SqlDbType.Int).Value = (
int_var==0)?(object)System.DBNull.Value:(object)int_var;

Note that you have to cast both parts of ? to object, otherwise you will
get an error, if you do not like to use ? use this

SqlParameter param = com.Parameters.Add("@forwarder", SqlDbType.Int);
if ( int_var==0)
param.Value = System.DBNull.Value
else
param.Value = int_var;



cheers,
 
Back
Top