469,903 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Empty field in Access

Hi

I am a beginner in VB.NET, and have a problem with empty field in Access

I have transfered a worksheet in Excel to Access table. Some of the cels are empty
I use VB.NET program to acces this Access table (product.mdb)

One of the statements is to validate whether the field empty or not
My statement is
If Trim(dr_Product("bt_m3event")) = "" Or Trim(dr_Product("bt_m3event")) = " " The
........
End I

dr_Product ==> is OleDbDataReade
bt_m3event ==>is the field nam

The bt_m3event is empty
When this statement is processed, I got a message like below

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dl
Additional information: Cast from type 'DBNull' to type 'String' is not valid

I have tried so many times and many ways, but it didn't work
Can anybody tell me how to validate an empty field

Thanks in advance

Joachim

Nov 20 '05 #1
3 2843
"Joachim" <an*******@discussions.microsoft.com> schrieb
Hi,

I am a beginner in VB.NET, and have a problem with empty field in
Access.

I have transfered a worksheet in Excel to Access table. Some of the
cels are empty. I use VB.NET program to acces this Access table
(product.mdb).

One of the statements is to validate whether the field empty or
not. My statement is :
If Trim(dr_Product("bt_m3event")) = "" Or
Trim(dr_Product("bt_m3event")) = " " Then
.........
End If

dr_Product ==> is OleDbDataReader
bt_m3event ==>is the field name

The bt_m3event is empty.
When this statement is processed, I got a message like below :

An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll Additional information: Cast from type
'DBNull' to type 'String' is not valid.

I have tried so many times and many ways, but it didn't work.
Can anybody tell me how to validate an empty field ?

First, you should enable Option Strict. Your code can not be compiled here
because the type of the DataReader's Item property is Object. You can not
compare every object to a string and you can't pass it to the Trim function.

As the exception message says, the field contains a Null value. The only
instance of Null in the Framework is DBNull.Value:

If dr_Product("bt_m3event") Is DBNull.Value Then
msgbox "Null found"
else
dim FieldContent as string
FieldContent = dr_Product("bt_m3event").ToString
end if

For all other date types, you must use type casting:

If dr_Product("AnyIntegerField") Is DBNull.Value Then
msgbox "Null found"
else
dim FieldContent as Integer
FieldContent = Directcast(dr_Product("bt_m3event"), Integer)
end if
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
* "=?Utf-8?B?Sm9hY2hpbQ==?=" <an*******@discussions.microsoft.com> scripsit:
I have transfered a worksheet in Excel to Access table. Some of the cels are empty.
I use VB.NET program to acces this Access table (product.mdb).

One of the statements is to validate whether the field empty or not.
My statement is :
If Trim(dr_Product("bt_m3event")) = "" Or Trim(dr_Product("bt_m3event")) = " " Then


'If dr_Product("bt_m3event") Is DBNull.Value Then...'. The right part of
the 'Or' IMO doesn't make sense at all because 'Trim' will remove the spaces.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Armin and Herfried
Thanks to you. It works
But I have another problem. I have posted it in the other thread
I think you can help me out.
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Wayno | last post: by
3 posts views Thread by tornado | last post: by
5 posts views Thread by Adrian Parker | last post: by
5 posts views Thread by Krechting | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.