Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

writing to a databasefrom a text file with asp - type mismatch

Hello.

Im trying to write from a comma delimited text file to a database. I have
the code, in fact ive tried two slightly different ways of doing this and
I always get the same problem: Type Mismatch. The problem seems to be with
separating the fields in the text file. I didt create the text file directly
from another database I simply put the data in myslef and put commas between
the first field and the second. I only have two fields as it is just a test
im doing before starting the real thing.
This is the code I am using. Ive marked the line where the error occcurs.

Dim DataConn, sql

Set DataConn = Server.CreateObject("ADODB.Connection")
DataConn.Open "testdb"

Dim objFSO, objFile, textfromfile

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Server.MapPath(".") & "/data.txt")

Dim arFields(2)

Do While Not objFile.AtEndOfStream
textfromfile = objFile.ReadLine
arFields = Split(textfromfile, ",")'THIS IS THE LINE WHERE THE ERROR OCCURS
sql = "INSERT INTO testtable (col1,col2) VALUES('" & arFields(0) & "', '"
& arFields(1) & "')"

conn.Execute(sql)
Loop
[1266 byte] By [JohnF] at [2007-11-9 17:50:44]
# 1 Re: writing to a databasefrom a text file with asp - type mismatch
John,

I also replied ot your duplicate entry in database.design tho it does not
belong there.

You are probably getting a type mismatch error because the Split method
creates an array from the argument passed to it. In your case you already
dim'd the variable as array. Instead just do this,

dim arFields

let me know if you still get errors.

--

Michael A Sanchez
Runtime Web Development
Dynamic Websites
Business Applications
Web: http://www.RuntimeWebDev.com
eMail: MSanchez@RuntimeWebDev.com

"JohnF" <johnfreeman@hotmail.com> wrote in message
news:3ef30393$1@tnews.web.dev-archive.com...
>
> Hello.
>
> Im trying to write from a comma delimited text file to a database. I have
> the code, in fact ive tried two slightly different ways of doing this and
> I always get the same problem: Type Mismatch. The problem seems to be with
> separating the fields in the text file. I didt create the text file
directly
> from another database I simply put the data in myslef and put commas
between
> the first field and the second. I only have two fields as it is just a
test
> im doing before starting the real thing.
> This is the code I am using. Ive marked the line where the error occcurs.
>
> Dim DataConn, sql
>
> Set DataConn = Server.CreateObject("ADODB.Connection")
> DataConn.Open "testdb"
>
> Dim objFSO, objFile, textfromfile
>
> Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(Server.MapPath(".") & "/data.txt")
>
> Dim arFields(2)
>
> Do While Not objFile.AtEndOfStream
> textfromfile = objFile.ReadLine
> arFields = Split(textfromfile, ",")'THIS IS THE LINE WHERE THE ERROR
OCCURS
> sql = "INSERT INTO testtable (col1,col2) VALUES('" & arFields(0) & "', '"
> & arFields(1) & "')"
>
> conn.Execute(sql)
> Loop
>
>
>
>
Michael at 2007-11-11 23:16:44 >
# 2 Re: writing to a databasefrom a text file with asp - type mismatch
John: Please do not post the same question to multiple groups. For more
information on dev-archive discussion group etiquette, please see
http://news.dev-archive.com/newspolicy.asp . Thanks!
--
Phil Weber
Phil Weber at 2007-11-11 23:17:49 >