Why am I getting this error : "ExecuteReader: Connection property has not been initialized." [migrated]
- by Olga
I'm trying to read .csv file to import its contents to SQL table
I'm getting error:
ExecuteReader: Connection property has not been initialized.
at the last line of this code:
Function ImportData(ByVal FU As FileUpload, ByVal filename As String, ByVal tablename As String) As Boolean
Try
Dim xConnStr As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};dbq=" & Path.GetDirectoryName(Server.MapPath(filename)) & ";extensions=asc,csv,tab,txt;"
' create your excel connection object using the connection string
Dim objXConn As New System.Data.Odbc.OdbcConnection(xConnStr.Trim())
objXConn.Open()
Dim objCommand As New OdbcCommand(String.Format("SELECT * FROM " & Path.GetFileName(Server.MapPath(filename)), objXConn))
If objXConn.State = ConnectionState.Closed Then
objXConn.Open()
Else
objXConn.Close()
objXConn.Open()
End If
' create a DataReader
Dim dr As OdbcDataReader
dr = objCommand.ExecuteReader()