Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
3,748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

597 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