Thursday 7 February 2013

Error Creating Data Table


Hi All just thought i would post this as i have just spent a bout an hour trying to work out why it would not work in my current project.

I have a  a function to create a data table and return it.


Public Function SQL2DataTable(ByVal sql As String)
        Dim SqlCon1 As String = "Data Source=sprintsql;Initial Catalog = database ;Integrated Security=SSPI;"
        Dim dt As New DataTable
        Dim da As New SqlClient.SqlDataAdapter(sql, SqlCon1)
        da.Fill(dt)
        da.Dispose()
        Return dt
    End Function

This is called like this

Dim dt As DataTable = SQL2DataTable(sql)

 now in this instance this would not work and came back with the error

    Unable to cast object of type 'System.Data.DataTable' to type 'Excel.DataTable'.

After a lot of frustration i found it was cause by having Imports Excel  declared at the beginning  Hope this saved you some time.


No comments:

Post a Comment

Passwords Made Easy

Today i thought i would write about password security. As we are all aware passwords are important but we continue to use very poor password...