Solved

Importing Excel Spreadsheet Data to SQL Server 2000 Database (using ASP), with field validation levels at each record. Also, unknown record count! Anyone?

Posted on 2004-08-03
15
3,739 Views
Last Modified: 2012-08-13
I am an ASP programmer and have adopted a ready made script to import data from an excel spreadsheet directly into a SQL server database, after certain field level validations.

The sample of a part of the script is shown below. My problem is:

1. I do not know the row count of the number of rows in the excel sheet, so apparently I have to select like roughly 500 rows in the excel sheet and name the entire range, some name, in my case called it "sheeter". My concern is if the data grows over 500 rows, what shall happen to my SQL statement which I am using to import the table range from excel, which is made of columns and rows (the matrix range). I want to dynamically grow my loop in some way to select more than the 500 if required. If I select the entire column range till the last excel row, then my PC crashes! I have to select a fix and valid range to query, which is logically absolute! Now I need help to grow this record count, till data exists. Now comes the question of empty rows : )

2. I need to validate each and every record and then directly insert the entire row using SQL statements to the database (SQL). My problem is memory and time! Any help on this then?

If anyone has some other alternative, function, procedure etc. please HELP OUT!

Points are your reward, and mine from my boss!

Thanx in advance!
0
Comment
Question by:Its_me_Rahul
15 Comments
 
LVL 7

Expert Comment

by:j2nku
ID: 11701358
Please, provide the sample - at least the part where you read the excel and do the looping.
0
 

Author Comment

by:Its_me_Rahul
ID: 11701453
Oh God I forgot, sorry!!! Here you go...


<%
' Selected constants from adovbs.inc
Const adOpenStatic = 3
Const adLockPessimistic = 2

Dim cnnExcel
Dim rstExcel
Dim I
Dim iCols

' This is all standard ADO except for the connection string.
' You can also use a DSN instead, but so it'll run out of the
' box on your machine I'm using the string instead.
Set cnnExcel = Server.CreateObject("ADODB.Connection")
cnnExcel.Open "DBQ=" & Server.MapPath("Intake_Sheet_2004.xls") & ";" & "DRIVER={Microsoft Excel Driver (*.xls)};"

' Same as any other data source.
' FYI: TestData is my named range in the Excel file
Set rstExcel = Server.CreateObject("ADODB.Recordset")
rstExcel.Open "SELECT * FROM sheeter;", cnnExcel, _
      adOpenStatic, adLockPessimistic

' Get a count of the fields and subtract one since we start
' counting from 0.
iCols = rstExcel.Fields.Count
%>
<table border="1">
      <thead>
            <%
            ' Show the names that are contained in the first row
            ' of the named range.  Make sure you include them in
            ' your range when you create it.
            For I = 0 To iCols - 1
                  Response.Write "<th>"
                  Response.Write rstExcel.Fields.Item(I).Name
                  Response.Write "</th>" & vbCrLf
            Next 'I
            %>
      </thead>
      <%
      rstExcel.MoveFirst

      ' Loop through the data rows showing data in an HTML table.
      Do While Not rstExcel.EOF
            Response.Write "<tr>" & vbCrLf
            For I = 0 To iCols - 1
                  Response.Write "<td>"
                  Response.Write rstExcel.Fields.Item(I).Value
                  Response.Write "</td>" & vbCrLf
            Next 'I
            Response.Write "</tr>" & vbCrLf

            rstExcel.MoveNext
      Loop
      %>
</table>

<%
rstExcel.Close
Set rstExcel = Nothing

cnnExcel.Close
Set cnnExcel = Nothing
%>
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 250 total points
ID: 11701592
You have basically two solutions
- use the sheet name instead of the range name (sheet name must be suffixed with a $ sign)

select * from [sheet_name$]

- create a dynamic named range instead of the static named range you have for the moment

In Excel, go to the Insert...Names menu
Select your sheeter named range
in the "refers to" field, type the following formula

=OFFSET($A$1,,,COUNTA($A:$A),12)

This will define a dynamic named range starting from cell A1, down to the last non-empty cell in the A column, 12 columns wide  (change the width to fit you own data)

HTH

Hilaire
0
 

Author Comment

by:Its_me_Rahul
ID: 11701601
I have ANOTHER VERY VERY serious problem! I can for ONCE compromise on the issue of performance and inserting rows and columns, but I am facing an unexpected error (below):

Error Type:
Provider (0x80004005)
Unspecified error
/eximport/Default.asp, line 17

in the file!! This is because of some kind of multiple user login problem or connection problem with the excel spredsheet through the webserver. If you can provide a solution for this first than anything else it is much better than the earlier problems!

Thanks again!
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11701604
Here a link to an excellent site,
you'll find more on how to define dynamic named ranges

http://www.cpearson.com/excel/named.htm

Hilaire
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11701675
By design an excel workbook is not intended to be shared.
You may face serious problems with concurrency/read locks.

Your best bet would be to upload the file to a database (Access, SQL Server) to avoid this kind of situation.

Also check that the file is not already opened by an Excel session.

Does the server.mappath resolve the path name ?
Is the file in the same virtual folder than the asp page, as the current path suggests ?

Also check connection.state after opening the connection.

What's on line 17 ??

Hilaire

0
 

Author Comment

by:Its_me_Rahul
ID: 11701741
Line 17 error:
cnnExcel.Open "DBQ=" & Server.MapPath("Intake_Sheet_2004.xls") & ";" & "DRIVER={Microsoft Excel Driver (*.xls)};"

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 26

Expert Comment

by:Hilaire
ID: 11702128
Can you issue a

Response.Write Server.MapPath("Intake_Sheet_2004.xls")

to check the path ?
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11702153
Also try to surround the path whith double quotes

cnnExcel.Open "DBQ="""& Server.MapPath("Intake_Sheet_2004.xls") & """;" & "DRIVER={Microsoft Excel Driver (*.xls)};"

Since the driver may not support paths/file names with spaces within it
0
 

Author Comment

by:Its_me_Rahul
ID: 11702171
No Hilaire, the reason for the error is due to the excel sheet getting locked out after one single import mechanism has run over it. I need to work a way round when no more errors occur due to the kind of locking!

Any one on that...?
0
 

Author Comment

by:Its_me_Rahul
ID: 11702254
Dear Hilaire, can you please elaborate a bit on checking the connection.state & checking if the file is locked or in closed state? If you explain a bit more on this?
0
 

Author Comment

by:Its_me_Rahul
ID: 11702272
I used the double quote suggestion you gave for the excel sheet connection path, and I found this:

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
[Microsoft][ODBC Excel Driver] Cannot update. Database or object is read-only.
/eximport/Default.asp, line 19
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11703141
try using adLockReadOnly    value=1
instead of adLockPessimistic
0
 

Author Comment

by:Its_me_Rahul
ID: 11745823
Thanks Hilaire...

I was away for a while over the weekend! Ur points are there now! Thanks loadz...

K
0
 

Expert Comment

by:WNottsC
ID: 12652414
Rahul, I am stuck on a similar problem.   I have looked at your code above and it show the contents of an excel spreadsheet on a web page but how do you then import that into a database?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now