Trouble with OleDb data extract

B

Brian Hanson

Hi,

I have an unusual problem that just showed its ugly head at a pretty
bad time. I have an asp.net (VB) app that takes data from an Excel
sheet and puts it into SQL Server. I get the data out of Excel using
OleDB, and suddenly, some of the data was not being extracted from
Excel.

I use OleDb for the extract into a DataTable and from there an
SqlClient.SqlCommand to put it into SQL Server.

I put the results of the OleDb extract into a datagrid to see if the
problem was there or the SqlClient insert. The datagrid showed
missing data, even before I got to the SQL insert. Here's my code
(roughly):

Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
_
& "Data Source = " test.xls" _
& ";" & "Extended Properties=Excel 8.0;"
Dim objConnXL As New OleDbConnection(sConnectionString)

''''Create Data Adapter and Data Set.
Dim Employee Select As New OleDbCommand("SELECT * FROM
Employee where RowNum > 0 ", objConnXL)
Dim Employee Adapter As New OleDbDataAdapter()
Employee Adapter.SelectCommand = Employee Select
Dim Employee Dataset As New DataSet()
Employee Adapter.Fill(Employee Dataset, "XLData")

'DataGrid1.DataSource = Employee Dataset.Tables(0).DefaultView
'DataGrid1.DataBind()


Assume the following data from an Excel namespace called Employee:

First Last Addr City State Zip RowNum
Joe Smith 123 Main NY NY 12345 1
Bill Jones 456 North LA CA 54321 2


The extract suddenly omitted the zip 12345, but displayed the other 13
pieces of data fine. I can see the data in the Excel sheet. This has
been working fine for several months, and of course, last week we went
live, go figure.

If anyone has run into this or anything like it, please give a yell.

Thanks.
 
J

Jacob Yang [MSFT]

Hi Brian,

I am not sure about the exact meaning of "The extract suddenly omitted the
zip 12345, but displayed the other 13 pieces of data fine." As I
understand, you mean that you lost the first row. Please remove "where
RowNum > 0" and test this issue again.

In addition, I believe that the following article is useful to you. Please
refer to it carefully.

Read Excel files from ASP.NET
http://www.aspfree.com/examples/766,1/examples.aspx
"...
This page provides a simple example of how to query an Excel spreadsheet
from an ASP.NET page using either C# or VB.NET.
..."

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian Hanson

Hi Jacob,

Thanks for the reply. What I meant by "The extract suddenly omitted the
zip 12345, but displayed the other 13 pieces of data fine” is this.

Assume the Excel data looking like this:

First Last Addr City State Zip RowNum
Joe Smith 123 Main NY NY 12345 1
Bill Jones 456 North LA CA 54321 2


Then assume the datagrid that selects everything from the Excel sheet
looks like this:

First Last Addr City State Zip RowNum
Joe Smith 123 Main NY NY 1
Bill Jones 456 North LA CA 54321 2


All data is selected from the Excel sheet except for one field.

Thanks.
 
O

onedaywhen

I assume by 'Excel namespace' you mean 'defined name' or 'named
range'. Have you checked its definition to ensure it is still
referencing the same Excel range i.e. including the header row?

BTW you should, for all sorts of reasons, seriously consider changing
your SQL from

SELECT * FROM ...

to

SELECT First, Last, Addr, City, State, Zip, RowNum FROM ...

You never know, it may help with the current problem.
 
B

Brian Hanson

Thank you both for your replies, here’s what I’ve learned in the last
few hours. First, by Excel namespace I did mean the named range, and it
has not changed. Also, I was using column names in my select vs. the
select * method.

What I have learned is the sequence of events while the user fills out
the Excel form is what triggers this, but I can’t assume that this is
the whole story. The column in question is originally set to datatype
Text. “It may be a number, but is displayed as entered.” We have a
button that executes the Cell.Clear function in VBA code. This was the
problem. It changed the column datatype from Text to General (and
possibly done more than that) now the OleDb extract will not read that
data.

We have fixed the problem going forward by using Cell.ClearContents
instead of Cell.Clear, but we still have existing Excel forms that I
need to read data from. I have manually switched the datatype from
General back to Text, but it still will not read the data. I have tried
to do a conversion from within the OleDb select statement, but it says
the value I am trying to convert is null gives me an error.

One other weird thing that I have noticed but am not sure is important.
When the data is originally entered into a field of type Text, it is
right justified in the cell. Once I do the Cell.Clear and it changes to
General data type, the data is left justified in the cell. I then
change the type back to Text, but the data remains left justified.
Final result, I still cannot get the data out of existing Excel sheets
via the OleDbCommand.

Thanks again.
 
J

Jacob Yang [MSFT]

Hi Brian,

Thank you for your update. It seems to be a strange issue.

Have you tested the example I mentioned before?

Read Excel files from ASP.NET
http://www.aspfree.com/examples/766,1/examples.aspx
"...
This page provides a simple example of how to query an Excel spreadsheet
from an ASP.NET page using either C# or VB.NET.
..."

Can you reproduce the same problem the above sample? I certainly appreciate
your time.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian Hanson

Hi,

FYI, we got our prob fixed & I hope this doesn't show twice. Setting
the IMEX =1 property of the connection string forced the data to be read
as text. Actually, it forces this registry setting:

'
hkey_local_machine|software\microsoft\jet\4.0\engines\excel\ImportMixedT
ypes.

It tells Excel how to treat mixed datatypes and was already set to text.
Apparently, the IMEX setting enforces the registry setting.


Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"_
& "Data Source = " test.xls" _
& ";" & "Extended Properties=""Excel 8.0; IMEX=1; """


That alone was giving an error of “Could not find installable ISAM”. It
was not untill the extra “” were added to the extended properties
section that it worked.



This was the post that worked for me:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=ebql
3v8ie02vcld5j7adc3p3pj9j3c0tp5%404ax.com&rnum=2&prev=/groups%3Fq%3Dimex%
2B%2B%2BCould%2Bnot%2Bfind%2Binstallable%2BISAM%2Boledb%26ie%3DUTF-8%26o
e%3DUTF-8%26hl%3Den%26btnG%3DGoogle%2BSearch
 
J

Jacob Yang [MSFT]

Hi Brian,

Thank you very much for sharing your solution. It is helpful to everybody
here.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top