Click to See Complete Forum and Search --> : Need Help with vb excel oledb


lew26
03-12-2006, 06:36 AM
hello,

I been working on this project for weeks now i can't seem to extract the date from an excel file using ADO.Net here is the code below. What i would like to accomplish is i would like to gather all the values from each row with the excel file and insert them into an array and a listbox. Then basically from there i can work with the array in other modules. The excel file contain three columns (Name, Phone, Email).


Every bit of help will be greatly appreciated.



Private Sub frmBulkEmailer_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

'Dim da As OleDb.OleDbDataAdapter = New Data.OleDb.OleDbDataAdapter
Dim dr As OleDb.OleDbDataReader
'Dim dp As OleDb.OleDbParameter = New Data.OleDb.OleDbParameter


'========================================
' oledb select
'========================================

Dim xdb As DBmConnect.ExcelmConn
Dim xlConn As String
xdb = New DBmConnect.ExcelmConn
xlConn = xdb.oledbExcelStandard("\xldoc.xls", "c:\docs")
'Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(xlConn)
Dim command As OleDb.OleDbCommand
Dim strSQL As String
Dim mName, mPhone, mEmail As String
Dim lstCustomers As ListBox = New ListBox


Try

'conn.Open()
'command.Connection = conn

'-- Create the SQL String
strSQL = "Select * FROM [sheet2]"

command = New OleDbCommand
With command
'-- Set up the connection of the command and the command text
.Connection = _
New OleDb.OleDbConnection(xlConn)
.Connection.Open()
.CommandText = strSQL

'-- Set up the data reader instance
dr = .ExecuteReader(CommandBehavior.SequentialAccess)
End With

'-- Add the items to the list box.
With Me.lbData
.Items.Clear()
.BeginUpdate()
Do While dr.Read
.Items.Add(dr.Item("Name"))
Loop
.EndUpdate()
End With

'command.CommandText = "SELECT Name FROM [sheet2] as @mName"
'command.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'conn.Close()
End Try

End Sub

pclement
03-12-2006, 04:08 PM
Might help us out a bit if you could identify where you are having a problem or are stuck.

lew26
03-12-2006, 04:56 PM
The problem i am having is when it get to this section in the code.

Do While dr.Read
.Items.Add(dr.Item("Name"))
Loop

what it does is bypasses it, it doesn't read the text in the spread sheet. It basically skips over it with out reading the information in the spreadsheet.

pclement
03-14-2006, 01:02 PM
Name is a reserved word is Jet OLEDB. Could that be what is causing the problem?

lew26
03-14-2006, 03:09 PM
I appreciate the response actually I did it wrong, someone pointed it out to me, it works now the name aslo works so that wasn't the problem.

Thanx for your reply