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

Null Database Values

Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute ""
where necessary?

Thanks,
Dan
Nov 20 '05 #1
6 2746
Dan
I check to see if the value is null before assiging. In my case with the
Reader object

If Not(rdr.IsDBNull(1)) Then myvar = rdr.GetString(1)
"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #2
You need to check for null first before assigning:

if not currentRow("FaxNumber") = DBNull.Value

end if

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #3
Compare the value to System.DBNull.Value:

If Not (your reference to the possible null) Is DBNull.Value Then
.....
'Assign

Else
....
'Handle the null case for example:
myVal = String.Empty
End if
--

Justin Weinberg
Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #4
"Dan Keeley" <ma********@hotmail.com> schrieb
This is probably not a dotnet specific issue, however i'm using
dotnet so here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type
String is not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and
substitute "" where necessary?

You could write a function to do it:

Function MyFunction(ByVal o As Object) As String
If o Is DBNull.Value Then
Return ""
Else
Return o.ToString
End If
End Function
As you want to substitue Null by a zero-length string, how do you
distinguish between Null and a zero-length string when you look at the
filled textbox?

--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Nov 20 '05 #5

Ok thanks for the replys, i'll probably go the adding a function route.

Rgds,
Dan

"Justin Weinberg" <jweinberg@_spamkill_mrgsoft.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
Compare the value to System.DBNull.Value:

If Not (your reference to the possible null) Is DBNull.Value Then
....
'Assign

Else
...
'Handle the null case for example:
myVal = String.Empty
End if
--

Justin Weinberg
Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute

""
where necessary?

Thanks,
Dan


Nov 20 '05 #6
I ran into this problem early on, so I ended up just adding a Check/Select
function pair for each data type. I know it's not the most efficient coding
method, but it works. Each Select function selects the value into an object
and returns the value of the Check function, which converts the object to
its specific data type, or returns 0 for numerics and "" for strings. Then
just put all of those functions in a global Query class or something along
those lines, and you can always access them easily.

Mike
Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:8J*****************@newsfep4-winn.server.ntli.net...
Hi,

This is probably not a dotnet specific issue, however i'm using dotnet so
here goes,.

I'm setting a text value of a field like so:

txtFax.Text = currentRow("FaxNumber")

However depending on the row, this value may be null.

This throws an exception because "Cast from type DBNULL to type String is
not valid"

Whats the proper way to deal with this?

Do i really have check the returned value for every field and substitute "" where necessary?

Thanks,
Dan

Nov 20 '05 #7

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

Similar topics

9
by: Joshua Ruppert | last post by:
A section of the documentation for the isSet() function states: Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant. Where would you encounter a NULL byte? Is a null...
6
by: Kris M | last post by:
How do i handle a null value for a date variable type. I am retrieving date data from an access database and storing the records in an array for processing. The array field has a date type and the...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
12
by: D Witherspoon | last post by:
What is the accepted method of creating a data class or business rules object class with properties that will allow the returning of null values? For example... I have a class named CResults with...
10
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable...
7
by: Dan | last post by:
I'm fairly new to C# and I am having some problems understanding int variables and null values. I have created a class that populates the values of it's fields from a DataReader in it's...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
4
by: qwedster | last post by:
Howdy folks! I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries. However how to check if a value (that is...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.