Code not working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I am trying to import an excel file into my vb.net app and populate a
datagrid. This is my code


Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim NewRecord As Boolean = False

Try

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=c:\sb1.xls; " & _
"Extended Properties=""Excel
8.0;IMEX=1;ReadOnly:=True;UpdateLinks:=0""")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [sheet1$]", MyConnection)

Dim dsi As DataSet
dsi = New System.Data.DataSet

MyCommand.Fill(dsi) -------------------------> stops here
MyConnection.Close()


grdStarBucks.DataSource = dsi.Tables(0)
Catch
End Try
MyConnection.Close()

It stops at MyCommand.Fill(dsi). Any ideas?

Thanks
 
The code is okay, I created an XLS with A,B,C,D,E on sheet 1, column 1 and
then did a writeXML on the dataset - This is what I got.

<?xml version="1.0" standalone="yes" ?>
- <NewDataSet>
- <Table>
<A>B</A>
</Table>
- <Table>
<A>C</A>
</Table>
- <Table>
<A>D</A>
</Table>
- <Table>
<A>E</A>
</Table>
</NewDataSet>

Are you sure you don't have a file locking issue?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
Please reply to the newsgroups instead of email so everyone can benefit from
your reply.
 
¤ Hi,
¤ I am trying to import an excel file into my vb.net app and populate a
¤ datagrid. This is my code
¤
¤
¤ Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
¤ Dim MyConnection As System.Data.OleDb.OleDbConnection
¤ Dim NewRecord As Boolean = False
¤
¤ Try
¤
¤ MyConnection = New System.Data.OleDb.OleDbConnection( _
¤ "provider=Microsoft.Jet.OLEDB.4.0; " & _
¤ "data source=c:\sb1.xls; " & _
¤ "Extended Properties=""Excel
¤ 8.0;IMEX=1;ReadOnly:=True;UpdateLinks:=0""")
¤ MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
¤ "select * from [sheet1$]", MyConnection)
¤
¤ Dim dsi As DataSet
¤ dsi = New System.Data.DataSet
¤
¤ MyCommand.Fill(dsi) -------------------------> stops here
¤ MyConnection.Close()
¤
¤
¤ grdStarBucks.DataSource = dsi.Tables(0)
¤ Catch
¤ End Try
¤ MyConnection.Close()
¤
¤ It stops at MyCommand.Fill(dsi). Any ideas?


I think you need to indicate what kind of error you are getting.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Hi,
The problem was a named sheet. It was sh_test1 instead of sheet1. How do i
set it to a named sheet?

Sahil Malik said:
The code is okay, I created an XLS with A,B,C,D,E on sheet 1, column 1 and
then did a writeXML on the dataset - This is what I got.

<?xml version="1.0" standalone="yes" ?>
- <NewDataSet>
- <Table>
<A>B</A>
</Table>
- <Table>
<A>C</A>
</Table>
- <Table>
<A>D</A>
</Table>
- <Table>
<A>E</A>
</Table>
</NewDataSet>

Are you sure you don't have a file locking issue?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
Please reply to the newsgroups instead of email so everyone can benefit from
your reply.


Chris said:
Hi,
I am trying to import an excel file into my vb.net app and populate a
datagrid. This is my code


Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim NewRecord As Boolean = False

Try

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=c:\sb1.xls; " & _
"Extended Properties=""Excel
8.0;IMEX=1;ReadOnly:=True;UpdateLinks:=0""")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [sheet1$]", MyConnection)

Dim dsi As DataSet
dsi = New System.Data.DataSet

MyCommand.Fill(dsi) -------------------------> stops here
MyConnection.Close()


grdStarBucks.DataSource = dsi.Tables(0)
Catch
End Try
MyConnection.Close()

It stops at MyCommand.Fill(dsi). Any ideas?

Thanks
 
Oh thats easy .. Sheet1 is the name, so it's Sheet1$, if the name is Chris,
the tablename is Chris$, if the name is Sahil, the tablename is Sahil$, if
the name is Donkey the tablename is Donkey$

I just tried it with AAAA and AAAA$ .. and it works !! :-)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
Please reply to the newsgroups instead of email so everyone can benefit from
your reply.


Chris said:
Hi,
The problem was a named sheet. It was sh_test1 instead of sheet1. How do i
set it to a named sheet?

Sahil Malik said:
The code is okay, I created an XLS with A,B,C,D,E on sheet 1, column 1 and
then did a writeXML on the dataset - This is what I got.

<?xml version="1.0" standalone="yes" ?>
- <NewDataSet>
- <Table>
<A>B</A>
</Table>
- <Table>
<A>C</A>
</Table>
- <Table>
<A>D</A>
</Table>
- <Table>
<A>E</A>
</Table>
</NewDataSet>

Are you sure you don't have a file locking issue?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
Please reply to the newsgroups instead of email so everyone can benefit from
your reply.


Chris said:
Hi,
I am trying to import an excel file into my vb.net app and populate a
datagrid. This is my code


Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim NewRecord As Boolean = False

Try

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=c:\sb1.xls; " & _
"Extended Properties=""Excel
8.0;IMEX=1;ReadOnly:=True;UpdateLinks:=0""")
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [sheet1$]", MyConnection)

Dim dsi As DataSet
dsi = New System.Data.DataSet

MyCommand.Fill(dsi) -------------------------> stops here
MyConnection.Close()


grdStarBucks.DataSource = dsi.Tables(0)
Catch
End Try
MyConnection.Close()

It stops at MyCommand.Fill(dsi). Any ideas?

Thanks
 
Back
Top