|
-
Oct 29th, 2010, 06:40 AM
#1
Thread Starter
Frenzied Member
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
-
Oct 29th, 2010, 06:50 AM
#2
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
-
Oct 29th, 2010, 08:49 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|