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. C#
  4. single quote error in SQL

single quote error in SQL

Scheduled Pinned Locked Moved C#
databaseregexhelpquestionannouncement
12 Posts 5 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
    emran834
    wrote on last edited by
    #1

    Hi, I am getting an error in SQL UPDATE statement when user enters single quote character in textbox. I used following regex to remove the single quote before submitting to SQL UPDATE Statement. But still SQL is catching that single quote everytime even after filtering from regex. Very wondering. Can you tell me why is this happening !! string question = rtxtInterViewQuestionSTAR.Text.Replace("\'", "").Trim(); string question = Regex.Replace(question, "\'", "", RegexOptions.Singleline | RegexOptions.Multiline).Trim(); UpdateCommand.CommandText = @"UPDATE star_interview_qa SET question ='" + question + "', strategy = '" + strategy + "', situation_task = '" + situation + "', action = '" + action + "', result = '" + result + "', sequence_no = " + sequence_noTextBoxSTAR.Text + " WHERE (id=" + lblIDqASTAR.Text + ")"; thanks and regards EMRAN

    C T 2 Replies Last reply
    0
    • E emran834

      Hi, I am getting an error in SQL UPDATE statement when user enters single quote character in textbox. I used following regex to remove the single quote before submitting to SQL UPDATE Statement. But still SQL is catching that single quote everytime even after filtering from regex. Very wondering. Can you tell me why is this happening !! string question = rtxtInterViewQuestionSTAR.Text.Replace("\'", "").Trim(); string question = Regex.Replace(question, "\'", "", RegexOptions.Singleline | RegexOptions.Multiline).Trim(); UpdateCommand.CommandText = @"UPDATE star_interview_qa SET question ='" + question + "', strategy = '" + strategy + "', situation_task = '" + situation + "', action = '" + action + "', result = '" + result + "', sequence_no = " + sequence_noTextBoxSTAR.Text + " WHERE (id=" + lblIDqASTAR.Text + ")"; thanks and regards EMRAN

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      emran834 wrote:

      I am getting an error in SQL UPDATE statement when user enters single quote character in textbox

      This is a symptom of code that is highly susceptable to a SQL Injection Attack. A way that a malicious user could attack your database through the application. You may want to read SQL Injection Attacks and Tips on How to Prevent Them[^] The above article will explain why this happens, and how to fix it and improve the security of your application's database access. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

      E 1 Reply Last reply
      0
      • E emran834

        Hi, I am getting an error in SQL UPDATE statement when user enters single quote character in textbox. I used following regex to remove the single quote before submitting to SQL UPDATE Statement. But still SQL is catching that single quote everytime even after filtering from regex. Very wondering. Can you tell me why is this happening !! string question = rtxtInterViewQuestionSTAR.Text.Replace("\'", "").Trim(); string question = Regex.Replace(question, "\'", "", RegexOptions.Singleline | RegexOptions.Multiline).Trim(); UpdateCommand.CommandText = @"UPDATE star_interview_qa SET question ='" + question + "', strategy = '" + strategy + "', situation_task = '" + situation + "', action = '" + action + "', result = '" + result + "', sequence_no = " + sequence_noTextBoxSTAR.Text + " WHERE (id=" + lblIDqASTAR.Text + ")"; thanks and regards EMRAN

        T Offline
        T Offline
        Thomas Lau
        wrote on last edited by
        #3

        I think that You'd better use parameters.:-D

        E 1 Reply Last reply
        0
        • C Colin Angus Mackay

          emran834 wrote:

          I am getting an error in SQL UPDATE statement when user enters single quote character in textbox

          This is a symptom of code that is highly susceptable to a SQL Injection Attack. A way that a malicious user could attack your database through the application. You may want to read SQL Injection Attacks and Tips on How to Prevent Them[^] The above article will explain why this happens, and how to fix it and improve the security of your application's database access. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          E Offline
          E Offline
          emran834
          wrote on last edited by
          #4

          Hi, thanks , I found the error and solved it. :)

          S 1 Reply Last reply
          0
          • T Thomas Lau

            I think that You'd better use parameters.:-D

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

            Hi, Even after using parameter i could not remove the error, but I found some place of my code where i needed to use REPLACE ' but i did not. Now its ok. Thanks

            1 Reply Last reply
            0
            • E emran834

              Hi, thanks , I found the error and solved it. :)

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              Fixing that error might solve your immediate problem, but leaves your code susceptible to SQL injection attacks. Do have a look at Colin's article (link in the post above). Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              E 1 Reply Last reply
              0
              • S S Senthil Kumar

                Fixing that error might solve your immediate problem, but leaves your code susceptible to SQL injection attacks. Do have a look at Colin's article (link in the post above). Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                E Offline
                E Offline
                emran834
                wrote on last edited by
                #7

                Hi Senthil , Thanks for your reply. I am developing C# for desktop small scale user application, so I thought, user could hack his own database, as long as it is not in a Server. By the way, I read your reply about Background Worker. you said it is great to work asynchronusly ... But cant i work Synchronusly with Background worker ? thanks Emran

                C S 2 Replies Last reply
                0
                • E emran834

                  Hi Senthil , Thanks for your reply. I am developing C# for desktop small scale user application, so I thought, user could hack his own database, as long as it is not in a Server. By the way, I read your reply about Background Worker. you said it is great to work asynchronusly ... But cant i work Synchronusly with Background worker ? thanks Emran

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  emran834 wrote:

                  user could hack his own database, as long as it is not in a Server.

                  Well, I would say that regardless of how the database is being used it is good to get in to the habit of coding in the way the article describes because it is then one less thing to worry about. Also, the database may not necessarily be running in the same user account as the application. This could lead to an elevation of privilage attack via the application's use of the database. Many databases allow some sort of access to the underlying operating system and although this can be restricted by the database administrator it often isn't, or the application logs on with all the privileges of the database administrator which mean that it is still possible to mount an elevation of privilege attack through a SQL Injection attack. In short, it is always best to defend your systems as much as possible because neither you nor I can forsee the uses that someone will put the application. What you feel is not a security issue maybe the tinyiest little gap in security that an attacker needs to widen the hole to let them have access to more and more of the system. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

                  1 Reply Last reply
                  0
                  • E emran834

                    Hi Senthil , Thanks for your reply. I am developing C# for desktop small scale user application, so I thought, user could hack his own database, as long as it is not in a Server. By the way, I read your reply about Background Worker. you said it is great to work asynchronusly ... But cant i work Synchronusly with Background worker ? thanks Emran

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    emran834 wrote:

                    By the way, I read your reply about Background Worker. you said it is great to work asynchronusly ... But cant i work Synchronusly with Background worker ?

                    Huh? Why would you need a component to run code synchronously? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    E 1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      emran834 wrote:

                      By the way, I read your reply about Background Worker. you said it is great to work asynchronusly ... But cant i work Synchronusly with Background worker ?

                      Huh? Why would you need a component to run code synchronously? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                      E Offline
                      E Offline
                      emran834
                      wrote on last edited by
                      #10

                      Hi, I think i am little bit confused with the word meaning of Synchronus and Asynchronus. To my thinking, Synchronus means At the same time the thread will be running without wiating for any other thread to complete theri task. And Asynchronus means The thread will wait for other threads to complete their task. Am i right ?

                      R 1 Reply Last reply
                      0
                      • E emran834

                        Hi, I think i am little bit confused with the word meaning of Synchronus and Asynchronus. To my thinking, Synchronus means At the same time the thread will be running without wiating for any other thread to complete theri task. And Asynchronus means The thread will wait for other threads to complete their task. Am i right ?

                        R Offline
                        R Offline
                        Roy Heil
                        wrote on last edited by
                        #11

                        Sorry. You got it backward. Synchronous means the first one talks, and the second one waits for it's turn. Then the second one talks, and the first one waits. They are synchronized. Asynchronous means they are not synchonized. Either side could be talking at any time. Roy.

                        E 1 Reply Last reply
                        0
                        • R Roy Heil

                          Sorry. You got it backward. Synchronous means the first one talks, and the second one waits for it's turn. Then the second one talks, and the first one waits. They are synchronized. Asynchronous means they are not synchonized. Either side could be talking at any time. Roy.

                          E Offline
                          E Offline
                          emran834
                          wrote on last edited by
                          #12

                          Excellent !! :) thats what I wanted

                          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