COMException occured while using ADODB

E

Edwinah63

Hi guys,

i have a collection containing the values and metadata about a current
record.
i build it by looping through the recordset as follows:


Public Function NoNull(ByVal Value As Object) As String
Return IIf(IsDBNull(Value), "", Value)
End Function

Public Function LoadFields(ByRef rs As ADODB.Recordset) As Collection
'returns fields of a single record
Dim fldCol As Collection
Dim p As PType
Dim i As Integer


fldCol = New Collection

Try
fldCol = New Collection
With rs
For i = 0 To .Fields.Count - 1
p = New PType
p.FieldName = .Fields(i).Name
p.Datatype = .Fields(i).Type
p.varValue = NoNull(.Fields(i).Value) '*** dies
here when no value
p.intLen = .Fields(i).DefinedSize()
fldCol.Add(p, p.FieldName)
Next i

End With

Catch e As Exception
Debug.WriteLine(e.Message)
fldCol = Nothing
End Try

Return fldCol

End Function


The above code works perfectly when i have a recordset that has both
fields and values,

but gives the following error when i have an empty recordset

error: an exception of type:
{System.Runtime.InteropServices.COMException} occurred

when i want to create a new record, i need to create an empty
collection that has the fields so i can populate then from the form
and send back to the database via a command parameters.

eg mycol = loadcollection(rs.open ("select * from mytbl where id =
-1"))

pls advise what is wrong with the code and what can i do to remedy
this?

regards

Edwinah63
 
C

Cor Ligthert

Hi Edwinah,

One of the main reasons dotnet is made is to prevent Com exceptions,
therefore you can use managed code.

That goes with in VB.net with ADONET a very fine set of methods, from which
almost everybody who have real tried it says that it is so much easier than
ADO to use.

It does not mean that the recordset is not processable in VB.net (I more and
more think that almost every classic Microsoft product is processable with
it), however ADONET fits better to VBNet. And because of that I get the idea
we more and more forget the classic methods from ADO.

Maybe it is a better idea if you try ADONET and the dataset.

I hope this helps?

Cor
 
E

Edwinah63

Hi Cor,

thanks for your thoughts, are you saying that the error is occuring
because of bugs in the way vbnet interprets ado??

i guess the question i should have asked above was why is an exception
raised at all when trying to assign a (presumably) null value.

can you give me some equivalent adonet code???

regards

Edwinah63
 
C

Cor Ligthert

Hi Edwinah,

Getting a dataset is very easy you need OleDB or Sqlclient
A connection with a connection string
www.connectionstrings.com

Create a datataadapter(SqlString, the connection)
A dataadapter with a fill
A dataset that is filled and than

And than something just as a sample, not your solution

Public Function LoadFields(ByVal ds As Dataset) As Collection
Dim fldCol As Collection
Dim p As PType
Dim i As Integer
fldCol = New Collection
Try
fldCol = New Collection
For i = 0 To ds.tables(0).count - 1
dim dr as datarow = ds.tables(0).rows(i)
p = New PType
p.FieldName = dr.("Name")
p.Datatype = dr.("Type")
Next
End Try
Return fldCol
End Function
///

I hope this gives an idea?

Cor
 
E

Edwinah63

Hi Cor,

thanks for your code. in light of my other problems i am having - i
may yet swap over from ado to adonet. i have to learn it anyway, so
now is probably a good time to start.

regards

Edwina
 

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

Similar Threads


Top