tags:

views:

729

answers:

2

I need help every time i run this code i get the same error

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

the code is

    Dim plmExcelCon As New System.Data.OleDb.OleDbConnection
    Dim cmdLoadExcel As System.Data.OleDb.OleDbDataAdapter
    Dim PrmPathExcelFile As String

    PrmPathExcelFile = txtImportFileLocation.Text.ToString

    plmExcelCon = New  System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +  PrmPathExcelFile + ";Extended Properties=Excel 12.0;")

    cmdLoadExcel = New System.Data.OleDb.OleDbDataAdapter("select * from [" + txtImportSheetName.Text + "$]", plmExcelCon)

    Dim ldExcelDS As System.Data.DataSet

    ldExcelDS = New System.Data.DataSet

    cmdLoadExcel.Fill(ldExcelDS)

    plmExcelCon.Close()

    'New
    Dim dt As DataTable = ldExcelDS.Tables(0)

Any help Most Appreciated

A: 

i have fixed the issue by filling the data table from cmdloadexcel

droyce
A: 

Although it appears that you have solved your problem (though I can't guess from your above code where DataView ever played a role at all), I'd like to give a brief answer to your original question.

You cannot convert a DataTable to a DataView through type-casting because the class System.Data.DataTable does not inherit from System.Data.DataView.

However, you can get a DataView instance of a DataTable object, e.g. through the latter's DefaultView property:

Dim dt As New DataTable

...

Dim dv As DataView = dt.DefaultView

See the MSDN library reference page for the DataTable.DefaultView property for details.

stakx