I've done some googling about my issue but I can't seem to find anything that works. I have a .csv (it is formatted just like an excel sheet, with just strings) that I am trying to import into a dataset, but only one of the columns is importing successfully. Everything else is DBNull.
Here is my dataset:
dset.Tables.Add(New DataTable With {.TableName = "ImportedData"})
dset.Tables("ImportDataTable").Columns.Add("ID", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Name", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Type", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc1", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc2", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc3", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc4", GetType(String))
And here is my import code:
Dim ImportFileCSV As New OpenFileDialog
With ImportFileCSV
.Title = "Import Overview"
.Filter = "CSV (*.csv)|*.csv"
End With
If ImportFileCSV .ShowDialog = DialogResult.OK Then
Dim ImportPath As String = ImportDetailedOverview.FileName
Dim ImportDirectoryPath As String = Path.GetDirectoryName(ImportPath) & ""
Dim ImportFileName As String = Path.GetFileName(ImportPath)
Using MyConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & ImportDirectoryPath & ";Extended Properties=""Text;HDR=YES;""")
Using MyCommand As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " & ImportFileName, MyConnection)
MyConnection.Open()
MyCommand.Fill(dset.Tables("ImportedData"))
MyConnection.Close()
End Using
End Using
Does anyone have any insight?
Edit:
Okay, so as per Ahmed, I am using the GenericParser:
Below code to select the file via OpenFileDialog and then to add the returned table to dset.Tables("ImportDataTable")
If ImportDetailedOverview.ShowDialog = DialogResult.OK Then
Dim ImportPath As String = ImportDetailedOverview.FileName
dset.Tables("ImportDataTable").Merge(ParseCSV(ImportPath))
I don't get any errors. It seems to finish successfully, but everything except Misc3 is blank. Through the power of binding sources, I am displaying that information to confirm:
CType(fBindingSource.Current, DataRowView)("Name") = drow("Name").ToString
MsgBox(drow("BuildingName").ToString)
CType(fBindingSource.Current, DataRowView)("Type") = drow("Type").ToString
CType(fBindingSource.Current, DataRowView)("Misc1") = drow("Misc1").ToString
CType(fBindingSource.Current, DataRowView)("Misc2") = drow("Misc2").ToString
CType(fBindingSource.Current, DataRowView)("Misc3") = drow("Misc3").ToString
CType(fBindingSource.Current, DataRowView)("Misc4") = drow("Misc4").ToString
The CSV is very simple, it's an Excel CSV, so for example: col1 has and ID field (numeric only) col2 has a name field (string) so on and so forth.
See Question&Answers more detail:
os