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. insert value error!!!

insert value error!!!

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestiondatabase
9 Posts 7 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.
  • H Offline
    H Offline
    hounetdev
    wrote on last edited by
    #1

    Hi, I am trying to insert a value into a access database field but the value i am tyring to insert has an ' in it and i think that is what is causing the problem. for example dataadapter.insertcommand.commandtext = "insert into table1 (clientname) values " & txtclient.Text & "" I think that if the user enters something with a ' in it i get an error. Can any one please let me know if i am right and how to fix it? Thank you, Santana

    E C D H 4 Replies Last reply
    0
    • H hounetdev

      Hi, I am trying to insert a value into a access database field but the value i am tyring to insert has an ' in it and i think that is what is causing the problem. for example dataadapter.insertcommand.commandtext = "insert into table1 (clientname) values " & txtclient.Text & "" I think that if the user enters something with a ' in it i get an error. Can any one please let me know if i am right and how to fix it? Thank you, Santana

      E Offline
      E Offline
      eramgarden
      wrote on last edited by
      #2

      you need to replace the single quote with 2 single quotes. insert into table1 (clientname) values ('o''neil')

      H 1 Reply Last reply
      0
      • E eramgarden

        you need to replace the single quote with 2 single quotes. insert into table1 (clientname) values ('o''neil')

        H Offline
        H Offline
        hounetdev
        wrote on last edited by
        #3

        What if i am getting the data from a textbox? Thank you, Santana

        G S 2 Replies Last reply
        0
        • H hounetdev

          Hi, I am trying to insert a value into a access database field but the value i am tyring to insert has an ' in it and i think that is what is causing the problem. for example dataadapter.insertcommand.commandtext = "insert into table1 (clientname) values " & txtclient.Text & "" I think that if the user enters something with a ' in it i get an error. Can any one please let me know if i am right and how to fix it? Thank you, Santana

          C Offline
          C Offline
          Charlie Williams
          wrote on last edited by
          #4

          Use parameters so you don't have to worry about escaping your input. It's also safer than concatenating SQL statements. Charlie if(!curlies){ return; }

          1 Reply Last reply
          0
          • H hounetdev

            What if i am getting the data from a textbox? Thank you, Santana

            G Offline
            G Offline
            Guerven
            wrote on last edited by
            #5

            do this first textbox1.text = replace(textbox1.text,"'","''") or replace(textbox1.text,"'","''") Marvin N. Guerrero - Casting More!!

            1 Reply Last reply
            0
            • H hounetdev

              Hi, I am trying to insert a value into a access database field but the value i am tyring to insert has an ' in it and i think that is what is causing the problem. for example dataadapter.insertcommand.commandtext = "insert into table1 (clientname) values " & txtclient.Text & "" I think that if the user enters something with a ' in it i get an error. Can any one please let me know if i am right and how to fix it? Thank you, Santana

              D Offline
              D Offline
              Dr_X
              wrote on last edited by
              #6

              Here you go. Just use these prior to saving or retrieving any text field.

                Friend Function SingleQuoteTextFromDB(ByVal text As Object) As String
                  'If a user saved a single quote in a text field, it was changed to
                  '2 single quotes. This function reverses the changes when displayed
                  If Not text Is Nothing Then
                    If Not text Is DBNull.Value Then
                      If InStr(text, "''") Then
                        Return Replace(text, "''", "'")
                      End If
                    End If
                  End If
                  Return text
                End Function
              
                Friend Function SingleQuoteTextToDB(ByVal text As String) As String
                  'If a user enters a single quote in a text field, 
                  'This will replace the single quote with 2 single quotes
                  If Not text Is Nothing Then
                    If Not text.Length.Equals(0) Then
                      If InStr(text, "'") Then
                        Return Replace(text, "'", "''")
                      End If
                    End If
                  End If
                  Return text
                End Function
              

              Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

              A 1 Reply Last reply
              0
              • D Dr_X

                Here you go. Just use these prior to saving or retrieving any text field.

                  Friend Function SingleQuoteTextFromDB(ByVal text As Object) As String
                    'If a user saved a single quote in a text field, it was changed to
                    '2 single quotes. This function reverses the changes when displayed
                    If Not text Is Nothing Then
                      If Not text Is DBNull.Value Then
                        If InStr(text, "''") Then
                          Return Replace(text, "''", "'")
                        End If
                      End If
                    End If
                    Return text
                  End Function
                
                  Friend Function SingleQuoteTextToDB(ByVal text As String) As String
                    'If a user enters a single quote in a text field, 
                    'This will replace the single quote with 2 single quotes
                    If Not text Is Nothing Then
                      If Not text.Length.Equals(0) Then
                        If InStr(text, "'") Then
                          Return Replace(text, "'", "''")
                        End If
                      End If
                    End If
                    Return text
                  End Function
                

                Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                The values aren't actually stored with the second quote, therefore values don't need to be decoded after being retrieved from the db.

                1 Reply Last reply
                0
                • H hounetdev

                  Hi, I am trying to insert a value into a access database field but the value i am tyring to insert has an ' in it and i think that is what is causing the problem. for example dataadapter.insertcommand.commandtext = "insert into table1 (clientname) values " & txtclient.Text & "" I think that if the user enters something with a ' in it i get an error. Can any one please let me know if i am right and how to fix it? Thank you, Santana

                  H Offline
                  H Offline
                  hounetdev
                  wrote on last edited by
                  #8

                  Thanks for all the help Guys, i used the replace method that Guerven suggested. Santana

                  1 Reply Last reply
                  0
                  • H hounetdev

                    What if i am getting the data from a textbox? Thank you, Santana

                    S Offline
                    S Offline
                    Steven Campbell
                    wrote on last edited by
                    #9

                    When you're done, please let me know the URL to your website, so that I can practise SQL injection attacks and delete your database. Thanks in advance, Steve :sigh:

                    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