Solved

vb.net sql command error

Posted on 2004-08-25
5
233 Views
Last Modified: 2010-04-17
I have a sql command that I get an error on the last entry my code looks like this



I get this error: on the last record only (it doesn't matter what it is. Now if I would take the last record and move it to the first it doesn't do it, but now the record I switch does it. So it is doing to the last record regardless of what the actual data is.)

Error: Synatax Error in string in query expression "

My code:
records is an array

                For t = 0 To records.Length
                    realdata= records(t)
                    command.CommandText = ""
                    command.CommandText = "Insert into table1(field1) values ('" & Replace$(realdata, "'", "''") & "')"
                    command.Connection = con
                    command.ExecuteNonQuery()
                next i


now the reason I have the replace function is because I have some ' in my data.

Now I eliminated the errors for the first couple of records, but everytime it hits the last record it gives me that error again. I used a debug.write(command.commandtext) and it looks like the insert line isn't putting in the ') at the end of the sql command.


so that is probrably what is wrong, but I cannot figure it out. Any ideas

why just the last record, any other ideas???

this is vb.net
0
Comment
Question by:bear23
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:adefwebserver
ID: 11894502
Perhaps try:
For t = 0 To records.Length - 1

0
 

Author Comment

by:bear23
ID: 11894545
no the array is reading fine

for so reason the insert line for the last record is leaving out the ')



and giving me that error

each line seems to work until it gets to the last record and then nothing

when I used debug the only thing I can see that is missing is the ') on the last record.

I even tried to manually enter it and nothing.


0
 
LVL 3

Expert Comment

by:adefwebserver
ID: 11894624
I would simply output the results of my "Replace$(realdata, "'", "''")" function so that I can see what data is causing it to crash.
0
 

Author Comment

by:bear23
ID: 11894663
I did:

>>>>>when I used debug the only thing I can see that is missing is the ') on the last record.



0
 
LVL 3

Accepted Solution

by:
adefwebserver earned 35 total points
ID: 11894774
I've had problems where there were control characters in my string. I think you have a "line feed" that is causing the last  ') not to appear.

Perhaps this will help:
http://www.vbcode.com/asp/showsn.asp?theID=4472
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

864 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

24 Experts available now in Live!

Get 1:1 Help Now