Solved

vb.net sql command error

Posted on 2004-08-25
5
236 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

733 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