Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Using SQL Search in c# page - problem with fields with ' ' in them

Using SQL Search in c# page - problem with fields with ' ' in them

Scheduled Pinned Locked Moved ASP.NET
helpcsharpdatabasequestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eyeseetee
    wrote on last edited by
    #1

    Hi I am using inline sql to search a table column. Some of the fields in the column have ' ' in them and whenever these fields are chosen it shows an error 'In correct Syntax near the text that is in the ' '. Is this a problem because it affects the sql syntax in the statement? My code goes something like 'select from table where field is LIKE '%" + name + "%'" Any help would be great. thanks Jamie

    S D N 4 Replies Last reply
    0
    • E eyeseetee

      Hi I am using inline sql to search a table column. Some of the fields in the column have ' ' in them and whenever these fields are chosen it shows an error 'In correct Syntax near the text that is in the ' '. Is this a problem because it affects the sql syntax in the statement? My code goes something like 'select from table where field is LIKE '%" + name + "%'" Any help would be great. thanks Jamie

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Could you post a bit of your code? This may help maybe... Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      1 Reply Last reply
      0
      • E eyeseetee

        Hi I am using inline sql to search a table column. Some of the fields in the column have ' ' in them and whenever these fields are chosen it shows an error 'In correct Syntax near the text that is in the ' '. Is this a problem because it affects the sql syntax in the statement? My code goes something like 'select from table where field is LIKE '%" + name + "%'" Any help would be great. thanks Jamie

        D Offline
        D Offline
        DoctorMick
        wrote on last edited by
        #3

        Try 'select from table where field is LIKE '%" + name.Replace("'", "''") + "%'" For a better, and safer, solution look into using parameters in your SQL.

        E 1 Reply Last reply
        0
        • E eyeseetee

          Hi I am using inline sql to search a table column. Some of the fields in the column have ' ' in them and whenever these fields are chosen it shows an error 'In correct Syntax near the text that is in the ' '. Is this a problem because it affects the sql syntax in the statement? My code goes something like 'select from table where field is LIKE '%" + name + "%'" Any help would be great. thanks Jamie

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          Oh I see, the value of name can contain ''... DoctorMick already gave the answer. But just one advice: Pay attention to sql injection (http://unixwiz.net/techtips/sql-injection.html[^]). You could use preparedstatements within your code. This will be the better (and more secure) way. Regards Sebastian

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          1 Reply Last reply
          0
          • D DoctorMick

            Try 'select from table where field is LIKE '%" + name.Replace("'", "''") + "%'" For a better, and safer, solution look into using parameters in your SQL.

            E Offline
            E Offline
            eyeseetee
            wrote on last edited by
            #5

            Ive replaced the apostrophies and it doesnt show an error message but now it just wont find that field which is weird. The sql statement is searching a table for fields that match the description the user puts in. Is this because the apostrophies are not recognised in the field? thanks OK forget what I said above. I think the problem is that there are breaks in the text in the database field, i.e. a new line so this might be causing the error of not finding it. Is this a known problem when searching database fields that if there is a line break in the field it affects finding data? thanks

            modified on Monday, June 29, 2009 8:26 AM

            1 Reply Last reply
            0
            • E eyeseetee

              Hi I am using inline sql to search a table column. Some of the fields in the column have ' ' in them and whenever these fields are chosen it shows an error 'In correct Syntax near the text that is in the ' '. Is this a problem because it affects the sql syntax in the statement? My code goes something like 'select from table where field is LIKE '%" + name + "%'" Any help would be great. thanks Jamie

              N Offline
              N Offline
              Niladri_Biswas
              wrote on last edited by
              #6

              Try this

              select from table where field is LIKE RTRIM(LTRIM('%" + name + "%'))"

              Hope this helps :)

              Niladri Biswas

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups