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. General Programming
  3. Visual Basic
  4. Use of apostrophe in string

Use of apostrophe in string

Scheduled Pinned Locked Moved Visual Basic
databasecollaborationhelp
4 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.
  • M Offline
    M Offline
    mcm
    wrote on last edited by
    #1

    The use of apostrophes in a textbox, when using collaboration with a SQL statement causes trouble - i was wondering if theres any way to get around this - ie when inserting a value that contains an apostrophe, to omit it before using the SQL insert statement. Any help would be greatly appreciated!

    P B R 3 Replies Last reply
    0
    • M mcm

      The use of apostrophes in a textbox, when using collaboration with a SQL statement causes trouble - i was wondering if theres any way to get around this - ie when inserting a value that contains an apostrophe, to omit it before using the SQL insert statement. Any help would be greatly appreciated!

      P Offline
      P Offline
      paul_b
      wrote on last edited by
      #2

      You can use StringVar=StringVar.Replace("'", "") to remove the apostrophe from your string (where StringVar is your variable). There may be something more elegant, but this has worked for me.

      1 Reply Last reply
      0
      • M mcm

        The use of apostrophes in a textbox, when using collaboration with a SQL statement causes trouble - i was wondering if theres any way to get around this - ie when inserting a value that contains an apostrophe, to omit it before using the SQL insert statement. Any help would be greatly appreciated!

        B Offline
        B Offline
        Brian Lehmann
        wrote on last edited by
        #3

        You may want to think about using a parameterized query to pass the values into your SQL statement. Using the SQL Server data provider, it would look something like this:

        Dim sql As String = "INSERT INTO Test (lname) VALUES (@LastName);" Dim cn As New SqlConnection(cnString) Dim cmd As New SqlCommand(sql, cn) cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = Me.TextBox2.Text cn.Open() cmd.ExecuteNonQuery() cn.Close()

        Doing it this way will save you the trouble of replacing characters in each input field and will help protect against some forms of SQL Injection attacks. However, it won't get you out of validating input for other constraints such as length, etc.

        Hope that helps!

        1 Reply Last reply
        0
        • M mcm

          The use of apostrophes in a textbox, when using collaboration with a SQL statement causes trouble - i was wondering if theres any way to get around this - ie when inserting a value that contains an apostrophe, to omit it before using the SQL insert statement. Any help would be greatly appreciated!

          R Offline
          R Offline
          RichardGrimmer
          wrote on last edited by
          #4

          If you're trying to insert into a database, just double-up the quotes, so : INSERT INTO table VALUES 'O'Connor' becomes INSERT INTO table VALUES 'O'**'**Connor' have some functions to do this, or just use a replace. This approach has the advantage that the string is stored with the apostrophe in place "Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox

          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