DAO Wrapper: Field is DBNull after Addnew and throws error

First a quick bit of background.

I have a legacy app that is written in VB6 using DAO. We are slowly migrating to .Net. DAO is a problem going forward, but we do not have the time (or $$) to rearchitect using ADO/ADO.Net. We decided to write a set of abstract VB.Net classes that present the DAO interface - this will allow us to implement concrete classes using any physical DB engine that we choose to use in the future. I currently have implemented the abstract DB interfaces as well as a concrete implementation for Jet. This works pretty well, as I simply pass through any call to the DAO equivalent.

However, I've recently come across a problem that is vexing me. Our legacy code declares variables of the new .Net classes .

dim rs as AbxRecordSet

and that allows the rest of the existing code to work without change.

rs.AddNew

rs.Fields("Field1").Value = "Foo"

However, the assignment fails with a "Data type conversion error". It turns out that in the Jet class, the value for rs.Fields("Field1").Value is DBNull, and executing the assignment causes the data conversion error.

I've implemented a simple test program that does the same essential operations using DAO and my wrappers, and I can see that in the case of DAO, the .Value property is similarly NULL before the assignment. The difference is that in the DAO case, it allows me to assign a new value, but in the wrapper case, it throws the data conversion error.

It seems like I ought to be able to make this assignment: the fact taht the value property is DBNull simply says that it has not been initialized, and I ought to be able to give it a value.

Is there something obvious (or non-obvious) that I'm missing

Thanks!

Sean



Answer this question

DAO Wrapper: Field is DBNull after Addnew and throws error

  • hessie

    The issue is not the current value of the field. Your custom classes are unable to convert from a .NET string type to the underlying type of Field1. Have you debugged into your AbxRecordSet class to find out exactly where the error is occurring

  • thesmileman

    Good job figuring it out :)
  • IanGilroy

    It was something obvious, and your post led me to the answer. The actual fields in the DB are typed, and I needed to be smarter about how I was assigning data into the fields. I've now modified my wrapper code to look at the field.type value and cast the inbound data accordingly. Works like a champ.

    It almost always is something obvious that I forget.

    Thanks for your reply.


  • DAO Wrapper: Field is DBNull after Addnew and throws error