Results 1 to 3 of 3

Thread: Error Message

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Error Message

    Hi, I have the following code which gives the error

    "Incorrect syntax near '='". Any help please

    Code:
    Private Sub populateOrgDetails()
    
            Dim cmd As New SqlCommand
            Dim conn As SqlConnection = GetDbConnection()
    
            Dim Orgdetails As New System.Data.SqlClient.SqlCommand(("Select OrgAdd1, OrgAdd2, OrgTown, OrgCountry From dbo.tbl_Organisation WHERE OrgID = " & _
            Me.txtclientOrgnw.Text & ""), conn)
    
            Using reader As System.Data.SqlClient.SqlDataReader = Orgdetails.ExecuteReader()
    
                While reader.Read()
    
                    Dim OrgAdd1 As String = FixNull(reader.GetValue(0))
                    Dim OrgAdd2 As String = FixNull(reader.GetValue(1))
                    Dim OrgTown As String = FixNull(reader.GetValue(2))
                    Dim OrgCountry As String = FixNull(reader.GetValue(3))
    
                    txtOrgAdd1.Text = OrgAdd1
                    txtOrgAdd2.Text = OrgAdd2
                    txtOrgTown.Text = OrgTown
                    txtOrgCountry.Text = OrgCountry
    
                End While
            End Using
    
        End Sub

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Error Message

    Remove the extra "" in the command text

    Code:
    Dim Orgdetails As New System.Data.SqlClient.SqlCommand(("Select OrgAdd1, OrgAdd2, OrgTown, OrgCountry From dbo.tbl_Organisation WHERE OrgID = " & _
            Me.txtclientOrgnw.Text ), conn)
    It is advisable to go for the SQLParameters rather than concatenating queries.
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Error Message

    It is definitely advisable to use parameters, but, if you don't want to, you must at least wrap your string in single quotes. This does not show up well when displayed as text, but it would look like this:

    WHERE OrgID = '" & Me.txtclientOrgnw.Text & "'"

    EDIT: Actually, now that I think about it, that will be true only for certain data types. I tend to use GUIDs for primary keys in the project I am working on, so any ID field would be wrapped in single quotes like this (as would strings), but if you are using integers, that won't matter. On the other hand, neither will that extra "" on the end if the type is actually an integer.

    So it depends on what type you are expecting.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width