473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

NULLs

Hi.

I have a database from which I am reading some integers into my C# app. The
problem is some of these DB values are NULL, so obviously an error is thrown
when I try to read them. Is there a standard way of representing a NULL
value type? Anyone got any tips on this?

Mike
Nov 16 '05 #1
12 1302
Hi Mike,

Check for DBNull(.Value)

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
> Hi Mike,

Check for DBNull(.Value)


Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL. How
do people get around this?

Mike
Nov 16 '05 #3
Well, for positive numbers, I would use -1. For both negative and positive numbers ... um ... maybe pick a number, any number and treat it as NULL, Int32.MinValue for instance.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

"Mike" <no***@hotmail.com> wrote in message
news:e4**************@tk2msftngp13.phx.gbl...
Hi Mike,

Check for DBNull(.Value)
Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL.

How do people get around this?

Mike


I think you are asking for nullable types which will be available in C# 2.0.
Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:

int? a = 5;
int? b = null;
int? c = a + b;

C would then contain null.

If you want nullable types today I guess you have to play around with a
DataSet.

Best Regards
- Michael S
Nov 16 '05 #5
What about SqlInt32 structure?
I use it in sqldatareader and have no problem with nulls.

--
Gawel
-------------------------------
Pierwszy łyk z pucharu nauk przyrodniczych czyni ateist±, ale na dnie
pucharu czeka Bóg.
Werner Heisenberg
Użytkownik "Mike" <no***@hotmail.com> napisał w wiadomo¶ci
news:e4**************@tk2msftngp13.phx.gbl...
Hi Mike,

Check for DBNull(.Value)
Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL.

How do people get around this?

Mike

Nov 16 '05 #6
> > Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL. How
do people get around this?

Mike


I think you are asking for nullable types which will be available in C#

2.0. Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:

int? a = 5;
int? b = null;
int? c = a + b;

C would then contain null.


That's exactly what I was asking, thanks.

Another question on nulls. I'm trying to override the == for a class I have
created:
public class Termination
{
public string CountryName, TerminationName;

public Termination(string countryName, string terminationName)
{
this.CountryName = countryName; this.TerminationName = terminationName;
}

public override string ToString()
{
string str = this.CountryName;
if(this.TerminationName != "" && this.TerminationName != null)
str += " - " + this.TerminationName;
return str;
}

// .................

public static bool operator==(Termination t1, Termination t2)
{
return (t1 == null && t2 == null) ||
(t1 != null && t2 != null && (String.Compare(t1.ToString(),
t2.ToString()) == 0));
}

// .................

}
This is all good if I do this:
Termination t1 = new Termination("USA", "Washington");
Termination t2 = new Termination("UK", "London");
Console.WriteLine(t1 == t2); // False
But this throws "An unhandled exception of type
'System.StackOverflowException'":
Termination t1 = null;
Console.WriteLine(t1 == null); // should be true
presumably because I am defining the == operator WITH the == operator,
causing infinite recursion at runtime. I originally tried this for my
override method:
public static bool operator==(Termination t1, Termination t2)
{
return (String.Compare(t1.ToString(), t2.ToString()) == 0));
}
but that gives "An instance of the object not set" at runtime when comparing
t1 to null.

How do I avoid this?

Mike
Nov 16 '05 #7
Hi Mike,
I havent run your code but I think that you are using your overloaded
ToString() method in the == method, then at the same time you are using
ToString() inside the == method, therefore you enter in an infinite cicle
where one method calls the other, hence the stack overflow exception :)
Cheers,

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

"Mike" <no***@hotmail.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
Yeah, I realise I can check for the NULL first, but I want to be able to represent this NULL value in my app. Like in the database, I can set an INTEGER field to NULL, but in C#, I can't set an int variable to NULL. How
do people get around this?

Mike


I think you are asking for nullable types which will be available in C#

2.0.
Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:

int? a = 5;
int? b = null;
int? c = a + b;

C would then contain null.


That's exactly what I was asking, thanks.

Another question on nulls. I'm trying to override the == for a class I

have created:
public class Termination
{
public string CountryName, TerminationName;

public Termination(string countryName, string terminationName)
{
this.CountryName = countryName; this.TerminationName = terminationName; }

public override string ToString()
{
string str = this.CountryName;
if(this.TerminationName != "" && this.TerminationName != null)
str += " - " + this.TerminationName;
return str;
}

// .................

public static bool operator==(Termination t1, Termination t2)
{
return (t1 == null && t2 == null) ||
(t1 != null && t2 != null && (String.Compare(t1.ToString(),
t2.ToString()) == 0));
}

// .................

}
This is all good if I do this:
Termination t1 = new Termination("USA", "Washington");
Termination t2 = new Termination("UK", "London");
Console.WriteLine(t1 == t2); // False
But this throws "An unhandled exception of type
'System.StackOverflowException'":
Termination t1 = null;
Console.WriteLine(t1 == null); // should be true
presumably because I am defining the == operator WITH the == operator,
causing infinite recursion at runtime. I originally tried this for my
override method:
public static bool operator==(Termination t1, Termination t2)
{
return (String.Compare(t1.ToString(), t2.ToString()) == 0));
}
but that gives "An instance of the object not set" at runtime when comparing t1 to null.

How do I avoid this?

Mike

Nov 16 '05 #8
Hi Mike,

If you google this you will find a lot of post related to this. Basically
you have two options ( the third being wait until C# 2.0 ships :) )
1- Use an especific integer value to indicate null , this is dependand of
your application .
2- Create/Use a wrapper class.

Cheers,

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

"Mike" <no***@hotmail.com> wrote in message
news:e4**************@tk2msftngp13.phx.gbl...
Hi Mike,

Check for DBNull(.Value)
Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL.

How do people get around this?

Mike

Nov 16 '05 #9
> Hi Mike,

I havent run your code but I think that you are using your overloaded
ToString() method in the == method, then at the same time you are using
ToString() inside the == method, therefore you enter in an infinite cicle
where one method calls the other, hence the stack overflow exception :)


Yes, my stack overfloweth.

Mike
Nov 16 '05 #10
Michael and Mike,

Don't forget that you can use the SqlInt32 structure as well, which has
null semantics.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael S" <a@b.c> wrote in message
news:Om**************@TK2MSFTNGP10.phx.gbl...

"Mike" <no***@hotmail.com> wrote in message
news:e4**************@tk2msftngp13.phx.gbl...
Hi Mike,

Check for DBNull(.Value)
Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL.

How
do people get around this?

Mike


I think you are asking for nullable types which will be available in C#

2.0. Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:

int? a = 5;
int? b = null;
int? c = a + b;

C would then contain null.

If you want nullable types today I guess you have to play around with a
DataSet.

Best Regards
- Michael S

Nov 16 '05 #11
> Michael and Mike,

Don't forget that you can use the SqlInt32 structure as well, which has null semantics.


Thank you Nicholas and Nick, it certainly does.

Mike
Nov 16 '05 #12

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ov**************@TK2MSFTNGP12.phx.gbl...
Michael and Mike,

Don't forget that you can use the SqlInt32 structure as well, which has null semantics.

Hope this helps.


Yes, a bit more lean method than my suggestion of using a whole bloated
DataSet! =)

Thanks
- Michael S
Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan Perlman | last post by:
From: "Dan Perlman" <dan@dpci.NOSPAM.us> Subject: ODBC creating nulls? Date: Friday, July 09, 2004 10:43 AM Hi, Below is my VB6 code that writes data from an Access 2000 table to a PG table....
2
by: Steve Walker | last post by:
Hi all. I've been tasked with "speeding up" a mid-sized production system. It is riddled with nulls... "IsNull" all over the procs, etc. Is it worth it to get rid of the nulls and not allow...
3
by: aaj | last post by:
Hi I am probably going to regret asking this because I'm sure you are going to tell me my design is bad 8-) ah well we all have to learn.... anyway I often use Nulls as a marker to see if...
6
by: mike | last post by:
I'm doing what I thought was a simple GROUP BY summary of fairly simple data and the my numbers aren't working out Some results are showing up <NULL> when I know the data is in the database ...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
13
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
3
by: Simon | last post by:
Hi all, Do you think the best way to avoid the problems of nulls in the database is just to provide default values via the db schema? Alternatively, is it better to allow nulls, seeing as the...
2
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
6
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.