none
[ADO.NET] Datatable.Load() method throws a System.FormatException RRS feed

  • Question

  • Hello everybody,

    I have a very strange problem with the load method of the datatable. I'm trying to fetch some data from an oracle view via a odbc connection. The code is the following:

               

     Public Function getOracleDataTable(ByVal connString As String, ByVal sqlText As String) As DataTable
            Dim dt As New DataTable
            Try
                Dim conn As OdbcConnection = New OdbcConnection
                conn.ConnectionString = connString
                Dim cmd As OdbcCommand = New OdbcCommand(sqlText)


                cmd.Connection = conn
                conn.Open()
                Dim reader As IDataReader
                reader = cmd.ExecuteReader()
    dt.Load(reader) HERE THROWS A FORMATEXCEPTION
                conn.Close()
                Return dt
            Catch ex As Exception
                MsgBox(Err.Description, MsgBoxStyle.Information, "---")
                Return Nothing
            End Try
        End Function

    and i really dont understand why. The query is correct and is like this "select * from tablename".

    If i do "select * from tablename where rownum<=3" than it loads the first 3 rows data on the datatable. If i do  "select * from tablename where rownum<=4" i geta formatexception error. It seems that the 4° row has some strange values but i have seen this rows and i dont understand where is the problem.

    Any idea? 

    thank you.


    I got the remedy



    • Edited by evald Tuesday, February 7, 2012 2:54 PM
    Tuesday, February 7, 2012 2:53 PM

Answers

  • Hi evald,

    Welcome to MSDN Forum.

    Please try to modify the T-SQL as 'select filed1, filed2...', don't use 'select *'. I think it may caused by incompatible schema. When you call dt.Load(reader) method, the datareader read data from database one record once and fill it to the datatable, I think the 4th record may have incompatible schema with first three records, so when you fill it to the datatable, the exception is thrown. You can also use DataAdapter to fill a datatable. More information about SqlDataAdapter, please refer this link. More information about DataTable.Load(IDataReader) method, please refer to this link.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, February 9, 2012 3:10 AM
    Moderator