i am trying to write an mdb to CVS converter with vb.net.
i want to get the columns header names using the oledbdata reader and here is my code so far
Public Cheadernames As Array
Public firstRead As Boolean = False
Public columnCount
Dim connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strfilename
Dim queryString = "SELECT * FROM products"
Dim connection As New OleDbConnection(connectionstring)
Dim command As New OleDbCommand(queryString, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read
If firstRead Then
columnCount = reader.FieldCount()
firstRead = False
For i = 0 To columnCount - 1
Cheadernames(i) = reader.GetName(i) ' i am getting an error with this line
Console.WriteLine(Cheadernames(i))
Next
End If
so i am trying to put all column headers names in an array but the commented line gives me this error “Object variable or With block variable not set”
Your Cheadernames variable is an empty array, with no elements and no type. That’s just wrong: you should almost never declare a variable or method parameter “As Array”. Instead, you want something like this: