datatable.import row bug/issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hharry

    #1

    datatable.import row bug/issue

    hello all,

    i have 2 datatables and am trying to transfer rows from datatable a to
    datatable b
    i use the datatable.impor trow method.

    the importrow method fails (but does not throw an exception) when
    importing a datarow that has numeric columns. columns of type string
    are imported fine.

    e.g.

    Dim objManualDataSe t As DataSet
    objMasterDataSe t.Tables.Add(Ne w DataTable(Table Names.Unique_Do B))

    objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    DataColumn("dob _key", System.Type.Get Type("System.St ring")))

    objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    DataColumn("mn" , System.Type.Get Type("System.In t16")))

    objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    DataColumn("dd" , System.Type.Get Type("System.In t16")))

    objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    DataColumn("yy" , System.Type.Get Type("System.In t16")))

    My 2nd dataset is generated by a call to a sql stored proc and has the
    same structure as the 1st dataset..

    My code to import the row is:

    For Each objRow As DataRow In objManualDataSe t.Tables(2).Row s
    objMasterDataSe t.Tables(TableN ames.Unique_DoB ).ImportRow(obj Row)
    Next

    This is the result:

    ? objManualDataSe t.Tables(2).Row s(0).ItemArray
    {Length=4}
    (0): "mn=1dd=12yy=19 55"
    (1): 1 {Short}
    (2): 12 {Short}
    (3): 1955 {Integer}
    ? objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Rows(0).ItemA rray
    {Length=4}
    (0): "mn=3dd=29yy=19 73"
    (1): {System.DBNull}
    (2): {System.DBNull}
    (3): {System.DBNull}

    Any ideas as to why cols 1, 2, 3 = System.DBNull ?

    Thanks in advance

  • Chris

    #2
    Re: datatable.impor t row bug/issue

    hharry wrote:[color=blue]
    > hello all,
    >
    > i have 2 datatables and am trying to transfer rows from datatable a to
    > datatable b
    > i use the datatable.impor trow method.
    >
    > the importrow method fails (but does not throw an exception) when
    > importing a datarow that has numeric columns. columns of type string
    > are imported fine.
    >
    > e.g.
    >
    > Dim objManualDataSe t As DataSet
    > objMasterDataSe t.Tables.Add(Ne w DataTable(Table Names.Unique_Do B))
    >
    > objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    > DataColumn("dob _key", System.Type.Get Type("System.St ring")))
    >
    > objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    > DataColumn("mn" , System.Type.Get Type("System.In t16")))
    >
    > objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    > DataColumn("dd" , System.Type.Get Type("System.In t16")))
    >
    > objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Columns.Add(N ew
    > DataColumn("yy" , System.Type.Get Type("System.In t16")))
    >
    > My 2nd dataset is generated by a call to a sql stored proc and has the
    > same structure as the 1st dataset..
    >
    > My code to import the row is:
    >
    > For Each objRow As DataRow In objManualDataSe t.Tables(2).Row s
    > objMasterDataSe t.Tables(TableN ames.Unique_DoB ).ImportRow(obj Row)
    > Next
    >
    > This is the result:
    >
    > ? objManualDataSe t.Tables(2).Row s(0).ItemArray
    > {Length=4}
    > (0): "mn=1dd=12yy=19 55"
    > (1): 1 {Short}
    > (2): 12 {Short}
    > (3): 1955 {Integer}
    > ? objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Rows(0).ItemA rray
    > {Length=4}
    > (0): "mn=3dd=29yy=19 73"
    > (1): {System.DBNull}
    > (2): {System.DBNull}
    > (3): {System.DBNull}
    >
    > Any ideas as to why cols 1, 2, 3 = System.DBNull ?
    >
    > Thanks in advance
    >[/color]

    I don't think you can do this. I seem to remember an issue that rows
    can not be attached to two datatables at once. I could be wrong though.
    Have you thought about cloning the datatable.

    DataTable.Clone

    Comment

    • hharry

      #3
      Re: datatable.impor t row bug/issue

      Chris,

      Yes, I tried using the Add method and got the row cannot belong to more
      than one datatable error. The ImportRow method worlks fine if all
      columns are of type string. I only get the error if any columns are
      numeric.

      Comment

      • hharry

        #4
        Re: datatable.impor t row bug/issue

        here is my workaround:

        For Each objRow As DataRow In objManualDataSe t.Tables(2).Row s
        Dim tmpRow(3) As Object
        tmpRow(0) = objRow.Item(0)
        tmpRow(1) = objRow.Item(1)
        tmpRow(2) = objRow.Item(2)
        tmpRow(3) = objRow.Item(3)

        objMasterDataSe t.Tables(TableN ames.Unique_DoB ).LoadDataRow(t mpRow,
        True)
        Next

        Comment

        • jayeldee

          #5
          Re: datatable.impor t row bug/issue

          You could try using something like this

          objMasterDataSe t.Tables(TableN ames.Unique_DoB ).Rows.Add(objR ow.ItemArray)

          in place of

          objMasterDataSe t.Tables(TableN ames.Unique_DoB ).ImportRow(obj Row)

          This call will depend on the Columns of both tables having the same
          indexes or it will error (Mismatched data types) or worse - it will
          copy the wrong data to the wrong columns.

          Comment

          Working...