473,396 Members | 2,076 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,396 software developers and data experts.

Nulls an string

How do you handle nulls and strings from Sql to a string value?

On a web page if the type is a VarChar going to a textbox, you can:

textbox.text = user["stringValue"];

But if an int value that could be null you need to test for DBNull.Value
first and just ignore it if it is a Null. This works fine for updating as
you can tell if the int is supposed to be null or not (if textbox empty -
null).

if I am reading into an int it is a pain as you have to make up a value for
null (usually 0, but not always) -as we talked about in another post:

int param7;
if (parameters[7] == DBNull.Value)
{
// ... handle null case ...
param7 = -1;
}
else
{
param7 = (int)(parameters[7]);
}

How do I handle a string?

I had thought, I wouldn't have to worry about it (like the textbox.text),
but that is not the case I still have to:

string param7;
if (parameters[7] == DBNull.Value)
{
// ... handle null case ...
}
else
{
param7 = (string)(parameters[7]);
}

What is the best way to handle a string? Make it "" if null?

Of course the problem is the same as int, if you send the data back to Sql,
you will now have a value in that field, that you may not want. You may
want it null. Dates are a good example here. I may not want a date in a
DateTerminated field if the person is still working.

Thanks,

Tom

Nov 17 '05 #1
1 1326
Hi,

What is the best way to handle a string? Make it "" if null?
Well, you can assign it to null, or just assign a predefined value,
depending of your app, example, I bind such a table to a datagrid, what I do
in that case is if the column is DBNull I show a "N/A" to the user.

Of course the problem is the same as int, if you send the data back to
Sql, you will now have a value in that field, that you may not want. You
may want it null. Dates are a good example here. I may not want a date
in a DateTerminated field if the person is still working.


The only solution using C# 1.X is either wrapping the int or DateTime in a
type that does support null, or using a predefined value as "null" ( -1 ,
DateTime.MinValue )
cheers,

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

Nov 17 '05 #2

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

Similar topics

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...
1
by: buc | last post by:
I have created a byte array (1000 in size) to recv a network packet. I recv the packet, which varies in size, and need to put the packet in a string; so I can append another string after the packet...
3
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hello, I've eliminated the bulk of code, this should be sufficient: byte fromEncrypt; string sDecryptedString; //Read the data out of the crypto stream. csDecrypt.Read(fromEncrypt, 0,...
2
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.