ado.net datatype typeOf issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I have a dataset and ask:
If TypeOf ds.Tables(0).Columns(i).DataType Is System.String Then
this returns FALSE

Typing
ds.Tables(0).Columns(i).DataType.UnderlyingSystemType.ToString
or
ds.Tables(0).Columns(i).DataType.ToString
"System.String"

ds.Tables(0).Columns(i).DataType.Name
"String"

ds.Tables(0).Columns(i).DataType.fullName
"System.String"

How come the typeOf evaluates FALSE?
 
That's because DataColumn.DataType returns a Type Object and not a String
Object and also a Type Object does not derive from the String Object. So,
TypeOf is bound to fail. You should rather do this:

If ds.Tables(0).Columns(i).DataType.FullName = GetType(String).FullName Then
' it is a string !
End If

You might also be able to do this:

If ds.Tables(0).Columns(i).DataType Is GetType(String) Then
' it is a string !
End If

The reason, I believe, being that there's only one type object in memory for
each type (atleast in a given AppDomain).

hope that helps..
Imran.
 
Thanks, this worked

If ds.Tables(0).Columns(i).DataType Is GetType(String) Then
' it is a string !
End If
 
Back
Top