Solved

error 102

Posted on 2013-10-30
4
303 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
[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
  • 2
  • 2
4 Comments
 
LVL 65

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

737 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