Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recordset Null Value Error 1

Status
Not open for further replies.

GhostWolf

Programmer
Jun 27, 2003
290
US
I need some help - please!!

I'm taking my first foray into VB.NET, by converting a working VB6 app to .NET. The application uses a DAO recordset from an Access database; most of the fields in the table being queried are string-data, and may be empty.

All the little problems it initially encountered, I've managed to straighten out - but this one: an empty field.

Running the program in debug, I sometimes get a "Cast from type 'DBNull' to type 'String' is not valid" error message that I just can't seem to get rid of.

Researching various posts here, I've tried
Code:
If .Fields("Comment").Value Is Not DBNull.Value Then
and
Code:
If NotNull(.Fields("Comment").Value) Then

<which is based on the following homemade function>
Private Function NotNull(ByVal prmStr As String) As Boolean
   NotNull = True
   If prmStr.Trim.Length = 0 Then NotNull = False
End Function

but still get the error message on some records.

I've tried variations of the old "IsNull", "Is Not Null" and "IsEmpty", along with the tests above; I've just about gone bug-eyed trying to find something in .NET help and the books I've got available to me about null values in recordsets - and I'm still stuck.

Could someone please enlighten me? How do I test for a null-value recordset field?
 
Try something like the following:

...If Not IsDBNull(.Fields("Comment").Value) Then

But the best advice anyone here would give you would be to forgo the DAO and move to ADO.Net. System.Data is one of the most common namespaces used in .Net by far.
 
Such a simple solution, and it works. Thank you RiverGuy.

Undoubtedly I'll end up moving to ADO.NET, but I think I'll work on learning the easy stuff first.
 
Ah, but it's what I've been using for some time now and am used to, so there's no learning curve on it right now. That means I can spend time on the language/functionality differences between VB6 and VB.NET before having to tackle connection strings and new Select/Insert/Update methods.

I'll get there eventually - and probably have even more basic questions than this on the way.
 
Well, the biggest difference between VB6 and .Net is the OO-ness of the latter.

So you should concentrate on that. If you don't, you will blame us for not telling you and insisting.

If you use VB.Net like you did VB6 then you should have stayed with VB6 or gone to some other language. You will not gain anything.

I know the learning curve will be high, but who cares, you will have to go through it sooner rather then later. And save yourself lots of time by doing it right the first time. Believe if been there. Allthough mine was access to .net, and lots of wasted time.

Christiaan Baes
Belgium

My Blog
 
That's what I'm trying to muddle my way through now, so I'll most likely have all kinds of silly questions before it's over.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top