Connecting Tech Pros Worldwide Forums | Help | Site Map

Int to Null ? Is possible ?

Paperback Writer
Guest
 
Posts: n/a
#1: Nov 17 '05
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 ?

--
Please, check my theSpoke:
http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx



Moty Michaely
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Int to Null ? Is possible ?


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 -

"Paperback Writer" <newsgroupms@gmail.com> wrote in message
news:Obr5wpydFHA.412@tk2msftngp13.phx.gbl...[color=blue]
> 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 ?
>
> --
> Please, check my theSpoke:
> http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx
>[/color]


Adam Clauss
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Int to Null ? Is possible ?


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.

---
Adam Clauss

"Paperback Writer" <newsgroupms@gmail.com> wrote in message
news:Obr5wpydFHA.412@tk2msftngp13.phx.gbl...[color=blue]
> 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 ?
>
> --
> Please, check my theSpoke:
> http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx
>[/color]


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Int to Null ? Is possible ?


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)in t_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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



"Paperback Writer" <newsgroupms@gmail.com> wrote in message
news:Obr5wpydFHA.412@tk2msftngp13.phx.gbl...[color=blue]
> 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 ?
>
> --
> Please, check my theSpoke:
> http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx
>[/color]


Closed Thread