473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading Excel into a dataset in VB.net

I haven't been able to configure a data adapter with the
wizard for Excel (Access is no problem) and when I try to
do it in code I still have problems, can anyone help,
here is some code I've been experimenting with.

Sub test()
Dim DS As System.Data.DataSet
Dim MyCommand As
System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As
System.Data.OleDb.OleDbConnection
Dim datagrid1 As DataGrid
MyConnection = New
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & _
"Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter
( _
"select * from [Sheet4$]", MyConnection)
DS = New System.Data.DataSet
MyCommand.Fill(DS)
datagrid1.SetDataBinding(DS, "sheet4$")
MyConnection.Close()
End Sub

Your help is greatly appreciated, Nitromuse
Nov 20 '05 #1
6 18068
Cor
Hi Nitromuse,

There are more errors than one and I do not know if I get them in once so I
have simplified your code
a litte bit otherwise we could not see the corrections no more.
(It where not that much errors the most important one was that you did mix
up the command and the dataadapter)
Sub test()
Dim datagrid1 As DataGrid
(this datagrid1 you have to add to the form also if you do it in this way,
better is just to drag it to your form first)

dim MyConnection as New
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & _
"Extended Properties=Excel 8.0;")
dim MyCommand as New System.Data.OleDbCommand ( _
"select * from [Sheet4$]", MyConnection)
dim myAdapter as new data.oledbadapter(mycommand) DS = New System.Data.DataSet
MyAdapter.Fill(DS)
datagrid1.SetDataBinding(DS, "sheet4$")
MyConnection.Close()
End Sub

I think we have the most now.

It was typed by hand, so if you have typos just change them and tell me if
you did succeed will you??

Cor
Nov 20 '05 #2
-----Original Message-----
Hi Nitromuse,

There are more errors than one and I do not know if I get them in once so Ihave simplified your code
a litte bit otherwise we could not see the corrections no more.(It where not that much errors the most important one was that you did mixup the command and the dataadapter)
Sub test()
Dim datagrid1 As DataGrid
(this datagrid1 you have to add to the form also if you

do it in this way,better is just to drag it to your form first)

dim MyConnection as New
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & _
"Extended Properties=Excel 8.0;")
dim MyCommand as New System.Data.OleDbCommand ( _
"select * from [Sheet4$]", MyConnection)
dim myAdapter as new data.oledbadapter(mycommand)
DS = New System.Data.DataSet
MyAdapter.Fill(DS)
datagrid1.SetDataBinding(DS, "sheet4$")
MyConnection.Close()
End Sub
I think we have the most now.

It was typed by hand, so if you have typos just change

them and tell me ifyou did succeed will you??

Cor
.
Here's what I ended up with to make debug happy and I

still get what I've always gotten since I started ....

Imports System.Data.OleDb

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

Dim DS As System.Data.DataSet
Dim MyCommand As
System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As New _
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & "Extended
Properties=Excel 8.0;")
'MyCommand = New
System.Data.oledb.OleDbDataAdapter
MyCommand = New System.Data.OleDb.OleDbDataAdapter
("select * from [Sheet4$]", MyConnection)

DS = New System.Data.DataSet
MyCommand.Fill(DS)
DataGrid1.SetDataBinding(DS, "sheet4$")
MyConnection.Close()
End Sub

End Class
An unhandled exception of
type 'System.Data.OleDb.OleDbException' occurred in
system.data.dll

Any further thoughts ???

Nov 20 '05 #3
BB
Nitromuse,

Here are a couple thoughts.

1. I'm not familiar with the [Sheet4$] syntax, but I've
had no problems with code very similar to yours and using
a named range directly--e.g. "select * from myrange".

2. Use a try/catch and then messagebox.show on
ex.tostring, and you'll get a more descriptive error
message. Might tell you something about the syntax of
your range.

3. For this and other interesting tidbits, check out Karl
Moore's excellent book The Ultimate VB.NET and ASP.NET
Code Book. The case of pulling excel ranges into
datasets is explained in detail (p. 174). I've had no
problems using his code verbatim.

hth,

Bill Borg
-----Original Message-----
-----Original Message-----
Hi Nitromuse,

There are more errors than one and I do not know if Iget them in once so I
have simplified your code
a litte bit otherwise we could not see the corrections

no more.
(It where not that much errors the most important one

was that you did mix
up the command and the dataadapter)
Sub test()
Dim datagrid1 As DataGrid


(this datagrid1 you have to add to the form also if you

do it in this way,
better is just to drag it to your form first)

dim MyConnection as New
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & _
"Extended Properties=Excel 8.0;")
dim MyCommand as New System.Data.OleDbCommand ( _
"select * from [Sheet4$]", MyConnection)
dim myAdapter as new data.oledbadapter(mycommand)
DS = New System.Data.DataSet
MyAdapter.Fill(DS)
datagrid1.SetDataBinding(DS, "sheet4$")
MyConnection.Close()
End Sub

I think we have the most now.

It was typed by hand, so if you have typos just change

them and tell me if
you did succeed will you??

Cor
.
Here's what I ended up with to make debug happy and I

still get what I've always gotten since I started ....

Imports System.Data.OleDb

Private Sub Form1_Load(ByVal sender As

System.Object,ByVal e As System.EventArgs) Handles MyBase.Load

Dim DS As System.Data.DataSet
Dim MyCommand As
System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As New _
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & "Extended
Properties=Excel 8.0;")
'MyCommand = New
System.Data.oledb.OleDbDataAdapter
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet4$]", MyConnection)

DS = New System.Data.DataSet
MyCommand.Fill(DS)
DataGrid1.SetDataBinding(DS, "sheet4$")
MyConnection.Close()
End Sub

End Class
An unhandled exception of
type 'System.Data.OleDb.OleDbException' occurred in
system.data.dll

Any further thoughts ???

.

Nov 20 '05 #4
Cor
Hi Nitromuse,

Is there a reason you delete the oledbcommand all the time from your code?
You are giving the dataAdapter the name command, but that is not enough I
think.
(There is a methode to put it in the OldeDbAdapter, but I see no reason for
that now, that makes it only more difficult to debug).
See my example again, you can also check your connection string, I mis
something in it but am not sure if that is necessary, here is a link. And
look on the other message please for the code I supported you about the
OleDbCommand and OleDbAdapter.

http://www.connectionstrings.com/

Cor
System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\Documents and
Settings\default\My Documents\Payroll.XLS; " & "Extended
Properties=Excel 8.0;")
'MyCommand = New
System.Data.oledb.OleDbDataAdapter
MyCommand = New System.Data.OleDb.OleDbDataAdapter
("select * from [Sheet4$]", MyConnection)

DS = New System.Data.DataSet
MyCommand.Fill(DS)

Nov 20 '05 #5
"BB" <an*******@discussions.microsoft.com> wrote in message news:<07****************************@phx.gbl>...
3. For this and other interesting tidbits, check out Karl
Moore's excellent book The Ultimate VB.NET and ASP.NET
Code Book.


Check out Karl's code at
http://www.developer.com/net/vb/article.php/3288031
Here's my Java version:

public static String getOrdinalString(long value)
{
String s = Long.toString(value);
if (value < 0) value = -value;
value = value % 100;
if (value < 4 || value > 20)
{
switch ((int)(value % 10))
{
case 1: return s + "st";
case 2: return s + "nd";
case 3: return s + "rd";
}
}
return s + "th";
}

Am I missing something?
Nov 20 '05 #6
Try this as well:

http://www.kjmsolutions.com/datasetarray.htm

"Mike" <ok*****@usa.net> wrote in message
news:e0**************************@posting.google.c om...
"BB" <an*******@discussions.microsoft.com> wrote in message news:<07****************************@phx.gbl>...
3. For this and other interesting tidbits, check out Karl
Moore's excellent book The Ultimate VB.NET and ASP.NET
Code Book.


Check out Karl's code at
http://www.developer.com/net/vb/article.php/3288031
Here's my Java version:

public static String getOrdinalString(long value)
{
String s = Long.toString(value);
if (value < 0) value = -value;
value = value % 100;
if (value < 4 || value > 20)
{
switch ((int)(value % 10))
{
case 1: return s + "st";
case 2: return s + "nd";
case 3: return s + "rd";
}
}
return s + "th";
}

Am I missing something?

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3104
by: Alex Shirley | last post by:
Hi I'm still banging my head on how to import an Excel XML spreadsheet into a dataset. The code will work for standard XML files, but not XML files made with Excel (I get 'Specified cast is not...
4
5182
by: Phoebe. | last post by:
Hi, Good Day! Reading 1 excel file into a dataset is fine. How can I read multiple excel with the same data structure into 1 dataset? How can I append those data? Can someone help? Thanks in...
5
8925
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
9
22475
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
3
8009
by: Brad | last post by:
I'm having a problem reading data from an Excel file into a dataset. Can anybody give me an idea of what's happening? I've included the problematic source and the error message to the end of this...
2
5603
by: rwiegel | last post by:
I'm trying to read rows from an Excel file and display them in an ASP.NET DataGridview. I am using C# for the code file. I am using OleDb to read from the Excel file. The columns that contain...
3
5913
by: stephen | last post by:
Hi, I have 5 excel files and they have multiple sheets. I have to read (say sheet 3) of each of the 5 excel files and consolidate them into one. what's the best way to achieve this. if someone...
1
10385
by: =?Utf-8?B?U2hlZXMgQWJpZGk=?= | last post by:
I read an article on the link: http://support.microsoft.com/default.aspx?scid=kb;en-us;306572 related to reading data from Excel using OLEDB The topic's heading is: How to query and display excel...
1
4672
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.