Empty field in Access

G

Guest

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
 
A

Armin Zingler

Joachim said:
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
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?Sm9hY2hpbQ==?= said:
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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top