Need to store a NULL in int32 - HOW???

  • Thread starter Thread starter Ali Baba
  • Start date Start date
A

Ali Baba

Hi,

Sorry, I am new to C# but know Java and C++. Question: I need to read
values from a database table using OleDb. I have a class (DAO pattern)
for every table where each field corresponds to a field in the database
table. I am using Int32 to store integer fields. However, since Int32 is
a Value type, I cannot store null in it. Which datatype can I use to
store a NULL for integers? In other words, is there a reference type in
C# that represents the Value types.

Currently I call IsDBNull to find out if the field is null, but don't
know how to store this in my DataAccessObject class.

In Java this is done by Integer, Float, ... classes.

Thanks,
AB
 
Hi,

Sorry, I am new to C# but know Java and C++. Question: I need to read values from a database table using OleDb. I have a class (DAO pattern) for every table where each field corresponds to a field in the database table. I am using Int32 to store integer fields. However, since Int32 is a Value type, I cannot store null in it. Which datatype can I use to store a NULL for integers? In other words, is there a reference type in C# that represents the Value types.

Currently I call IsDBNull to find out if the field is null, but don't know how to store this in my DataAccessObject class.

In Java this is done by Integer, Float, ... classes.

Thanks,
AB

Here is an excerpt from the C# help in Visual Studio .NET found by searching for "integer NULL value"
The entire code block is fairly long, so I won't quote the whole thing.

It begins:

"The DBInt struct below implements an integer type that can represent the complete set of values of the int type, plus an additional state that indicates an unknown value. A type with these characteristics is commonly used in databases."

If you cannot locate the article, entitled "Database Integer Type" from the C# language specification, let me know and I'll send it to you via email.
The URL is: ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/csspec/html/vclrfcsharpspec_11_4_1.htm
 
0 (zero) :D
Hi,

Sorry, I am new to C# but know Java and C++. Question: I need to read values from a database table using OleDb. I have a class (DAO pattern) for every table where each field corresponds to a field in the database table. I am using Int32 to store integer fields. However, since Int32 is a Value type, I cannot store null in it. Which datatype can I use to store a NULL for integers? In other words, is there a reference type in C# that represents the Value types.

Currently I call IsDBNull to find out if the field is null, but don't know how to store this in my DataAccessObject class.

In Java this is done by Integer, Float, ... classes.

Thanks,
AB
 
You could set your value equal to DBNull.Value. Your variable would need to
be something generic, such as an Object reference, which will be able to
hold the DBNull, or any valid integer that you want to assign to it.
 
Peter,

Thank you for your repsonse. I found the article however, would like to
know if there is a built-in type rather than creating a new class. I did
find a class called System.Data.SqlTypes.SqlInt32, which will tell me if
the value is null, but cannot figure out how can I set a null for the
object. I need to do the following:


SqlInt32 i;

.... do some work here

//Now I need to set this to null
i.setNull(); //There is no such method.

//Following does not work since SqlInt32 is also a value type;
i = null; //Won't work



Problem 2: The documents say that SqlInt32 datatype is for SQL server. I
am using OleDb to fetch data, which will be used for databases other
than SQL server. I don't know what will happen in this case.

AB
 
0 does not mean NULL



news.microsoft.com said:
0 (zero) :D

"Ali Baba" <[email protected]
Hi,

Sorry, I am new to C# but know Java and C++. Question: I need to
read values from a database table using OleDb. I have a class (DAO
pattern) for every table where each field corresponds to a field
in the database table. I am using Int32 to store integer fields.
However, since Int32 is a Value type, I cannot store null in it.
Which datatype can I use to store a NULL for integers? In other
words, is there a reference type in C# that represents the Value
types.

Currently I call IsDBNull to find out if the field is null, but
don't know how to store this in my DataAccessObject class.

In Java this is done by Integer, Float, ... classes.

Thanks,
AB
 
Ali,
//Following does not work since SqlInt32 is also a value type;
i = null; //Won't work
To set any of the types in System.Data.SqlTypes to a database NULL use:

i = SqlInt32.Null;

Where you change SqlInt32 to the respect field type.

Also, as I am sure you know a Database NULL is not the same as a null
reference, generally a Database NULL is represented by System.DBNull.Value
in .NET, the SqlTypes types have intrinsic support for Database NULL, by
passing the need for System.DBNull. Of course the System.Data.SqlClient has
better support for SqlTypes then the OleDb client.

Hope this helps
Jay


Ali Baba said:
Peter,

Thank you for your repsonse. I found the article however, would like to
know if there is a built-in type rather than creating a new class. I did
find a class called System.Data.SqlTypes.SqlInt32, which will tell me if
the value is null, but cannot figure out how can I set a null for the
object. I need to do the following:


SqlInt32 i;

... do some work here

//Now I need to set this to null
i.setNull(); //There is no such method.

//Following does not work since SqlInt32 is also a value type;
i = null; //Won't work



Problem 2: The documents say that SqlInt32 datatype is for SQL server. I
am using OleDb to fetch data, which will be used for databases other
than SQL server. I don't know what will happen in this case.

AB




ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/csspec/html/vclrfcsharpspec_1
1_4_1.htm

--

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free
<http://www.spammarshall.com>




----------------------------------------------------------------------------
----


Peter,

Thank you for your repsonse. I found the article however, would like to
know if there is a built-in type rather than creating a new class. I did
find a class called System.Data.SqlTypes.SqlInt32, which will tell me if the
value is null, but cannot figure out how can I set a null for the object. I
need to do the following:
SqlInt32 i;

... do some work here

//Now I need to set this to null
i.setNull(); //There is no such method.

//Following does not work since SqlInt32 is also a value type;
i = null; //Won't work



Problem 2: The documents say that SqlInt32 datatype is for SQL server. I
am using OleDb to fetch data, which will be used for databases other than
SQL server. I don't know what will happen in this case.
AB





Peter van der Goes wrote:


Hi,

Sorry, I am new to C# but know Java and C++. Question: I need to read
values from a database table using OleDb. I have a class (DAO pattern) for
every table where each field corresponds to a field in the database table. I
am using Int32 to store integer fields. However, since Int32 is a Value
type, I cannot store null in it. Which datatype can I use to store a NULL
for integers? In other words, is there a reference type in C# that
represents the Value types.
Currently I call IsDBNull to find out if the field is null, but don't
know how to store this in my DataAccessObject class.
In Java this is done by Integer, Float, ... classes.

Thanks,
AB

Here is an excerpt from the C# help in Visual Studio .NET found by
searching for "integer NULL value"
The entire code block is fairly long, so I won't quote the whole thing.

It begins:

"The DBInt struct below implements an integer type that can represent
the complete set of values of the int type, plus an additional state that
indicates an unknown value. A type with these characteristics is commonly
used in databases."
If you cannot locate the article, entitled "Database Integer Type"
from the C# language specification, let me know and I'll send it to you via
email.
 
Back
Top