none
How to Convert String Property Fields into the Datatype that Matches the Datacolumn in the Datatable? RRS feed

  • Question

  • I am creating a VB.NET Windows application. At one point in the program, I need to assign the string property values of an object to the field in a typed datarow, which was created with the NewRow method on the typed datatable in a dataset. I need to dynamically convert the property value to the datatype of the column in which I am inserting the value.

    I loop through the properties of the object (there are 287 of them) and only pull out the ones that I want. When it discovers a match, it attempts to assign the property value to the item in the typed datarow with the same name. Of course, string values into a string column field gets assigned without error; however, when a string, like '32', is assigned to a field defined with an integer datatype, the following exception occurs:

    "Input string was not in a correct format. Couldn't store <field> in <columnName> column. Expected type is <datatype>."

    The object is a file in a FolderItem from the Shell32 class.

    I have tried using CType, CTypeDynamic and DirectCast. The CTypeDynamic works best, but I need to feed it the correct datatype of the column I am about to assign it to. My list of fields required is not long, so I know that I could use a select statement, but surely there must be a way for it to figure out what datatype to convert the string to.


    'properties: an earlier loop fetches all the property names from the object
    'folder and fileName: the folder and file being examined
    'rw: the new row created from the typed datatable
    'g_fileFields: the list I fields I want to obtain values from

    Private Sub GetFileProperties(ByRef properties As ArrayList, _ ByRef folder As Shell32.Folder, _ ByVal fileName As String, _ ByRef rw As FilesRow) 'now fetch the properties for each file For i = 0 To properties.Count - 1 If g_fileFields.Contains(properties(i)) Then Dim colType As Type = rw.Table.Columns(properties(i)).GetType.ReflectedType '<-- gets type = column 'rw.Item(properties(i)) = CTypeDynamic(folder.GetDetailsOf(fileName, i), colType) <-- I tried this rw.Item(properties(i)) = folder.GetDetailsOf(fileName, i) '<-- error happens here End If Next End Sub



    Thursday, April 26, 2012 6:35 AM

Answers

  • hi,

    rw.Table.Columns(properties(i)).datatype

    gives you the target datatype.


    Regards, Nico

    • Marked as answer by em-squared Thursday, April 26, 2012 8:35 AM
    Thursday, April 26, 2012 8:22 AM

All replies

  • hi,

    rw.Table.Columns(properties(i)).datatype

    gives you the target datatype.


    Regards, Nico

    • Marked as answer by em-squared Thursday, April 26, 2012 8:35 AM
    Thursday, April 26, 2012 8:22 AM
  • Thanks, Nico. I did try that at one point and it did not resolve the datatype, but I must have changed something else because it works now. I thought that because the Datatype property did not show up in the Intellisense, I mustn't have access to it for some reason.
    Thursday, April 26, 2012 8:35 AM