Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

error 102

Posted on 2013-10-30
4
Medium Priority
?
314 Views
Last Modified: 2013-10-31
Problem using Execute Reader
in this part of the program I want to see if there are similar names to  (vemp)  in a table.
 
Get Error number 102  " incorrect syntax near '%'  

vemp = InputBox("Digit Company name", "")
        If vemp.ToString <> "" Then

            Dim sSql As String
            Dim objConn As SqlConnection
            Dim objCmd As SqlCommand
            Dim DR As SqlDataReader
            Dim strConn As String = "server=.\SqlExpress; database=mala2; Trusted_Connection=yes"
            objConn = New SqlConnection(strConn)
            sSql = "select * from md1 where empresa like " & vemp & "%"
            objCmd = New SqlCommand(sSql, objConn)
            Try
                objConn.Open()
            Catch exc As Exception
                MessageBox.Show("Error in opening the  connection")
                Exit Sub
            End Try

ok up to here

            Try
                DR = objCmd.ExecuteReader
            Catch exc As SqlException
                Dim erro As String
                error = exc.Number.ToString + " - " + exc. Message
                MessageBox.Show(error)
                Exit Sub
            End Try

            If DR.HasRows Then  ' there are similar names, show table
                Me.MD1_CombTableAdapter.FillBy1(Me.Mala2DataSet.MD1_Comb, vemp.ToString)
                If MsgBox("Please see similar existing names, want to continue?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                    Exit Sub
                Else
            .........
                End If

                 .........
            Else
                ........

            End If
            DR.Close()
0
Comment
Question by:dovk179
  • 2
  • 2
4 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39611332
Assuming that empresa is a string, you need to pass single quotes around the vemp variable and wildcard %, like...

sSql = "select * from md1 where empresa like '" & vemp & "%'"

Open in new window

0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 1500 total points
ID: 39611339
You missed the single quotes around the variable

sSql = "select * from md1 where empresa like '" & vemp & "%'"

When debugging these things, output the sSql string to the screen and then copy and paste it into SQL to test it.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39612626
dovk179 - Curiosity overwhelms me ... did you try my solution?
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39613542
Wow yeah. jimhorn kinda beat me to it by a few minutes. He should get the points, and to be honest the points should be a grade A as that solved your problem completely with example code.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

810 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