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. How do I include textbox data in SQL query?

How do I include textbox data in SQL query?

Scheduled Pinned Locked Moved C#
databasehelpquestion
7 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.
  • J Offline
    J Offline
    Joe Stansfield
    wrote on last edited by
    #1

    Hi, This is most likely a basic problem but I can't get my head around it! I am trying to construct a parameterized sql query that takes information entered from a textbox (the search box of the query) and adds it to the query before it executes. What I would like to do is have the user search the 'CODE' column by entering something like: = "CO" or "SO" and have that added to my sql query after WHERE CODE Any help greatly appreciated! Joe

    J P P B 4 Replies Last reply
    0
    • J Joe Stansfield

      Hi, This is most likely a basic problem but I can't get my head around it! I am trying to construct a parameterized sql query that takes information entered from a textbox (the search box of the query) and adds it to the query before it executes. What I would like to do is have the user search the 'CODE' column by entering something like: = "CO" or "SO" and have that added to my sql query after WHERE CODE Any help greatly appreciated! Joe

      J Offline
      J Offline
      JHizzle
      wrote on last edited by
      #2

      Erm. AFAIK the whole point of a parameterized query was to stop this kind of design so that's not going to work. You'd have to go back to the old skool method of generating the entire SQL statement and firing it back to your DB. I'd check if you really need to as this is horrendously insecure. Can you just not create a search stored proc and pass in a search term on that?

      J 1 Reply Last reply
      0
      • J Joe Stansfield

        Hi, This is most likely a basic problem but I can't get my head around it! I am trying to construct a parameterized sql query that takes information entered from a textbox (the search box of the query) and adds it to the query before it executes. What I would like to do is have the user search the 'CODE' column by entering something like: = "CO" or "SO" and have that added to my sql query after WHERE CODE Any help greatly appreciated! Joe

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        It depends on the type of database you are using as to how you tackle this, but you could add a temporary table that you use to add the individual parsed out elements in as individual elements, and then perform your main query using that table as a linked table, so you'd end up with SQL along the lines of:

        SELECT ... FROM MyTable INNER JOIN MyTemporaryTable ON MyTable.Code = MyTemporaryTable.Code

        Also, rather than having them enter the codes, why not display them in a list that they can select from? This way you save them having to remember potentially archane code sequences.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        1 Reply Last reply
        0
        • J Joe Stansfield

          Hi, This is most likely a basic problem but I can't get my head around it! I am trying to construct a parameterized sql query that takes information entered from a textbox (the search box of the query) and adds it to the query before it executes. What I would like to do is have the user search the 'CODE' column by entering something like: = "CO" or "SO" and have that added to my sql query after WHERE CODE Any help greatly appreciated! Joe

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Something like cmd.Parameters [ "@CodeVal" ].Value = tbCode.Text ; ?

          1 Reply Last reply
          0
          • J Joe Stansfield

            Hi, This is most likely a basic problem but I can't get my head around it! I am trying to construct a parameterized sql query that takes information entered from a textbox (the search box of the query) and adds it to the query before it executes. What I would like to do is have the user search the 'CODE' column by entering something like: = "CO" or "SO" and have that added to my sql query after WHERE CODE Any help greatly appreciated! Joe

            B Offline
            B Offline
            brunoseixas
            wrote on last edited by
            #5

            where CODE like '%' + textbox.text + '%' ???

            P 1 Reply Last reply
            0
            • B brunoseixas

              where CODE like '%' + textbox.text + '%' ???

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Apart from being wide open to a SQL injection attack, this just won't work because it only applies to 1 value.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              • J JHizzle

                Erm. AFAIK the whole point of a parameterized query was to stop this kind of design so that's not going to work. You'd have to go back to the old skool method of generating the entire SQL statement and firing it back to your DB. I'd check if you really need to as this is horrendously insecure. Can you just not create a search stored proc and pass in a search term on that?

                J Offline
                J Offline
                Joe Stansfield
                wrote on last edited by
                #7

                Thanks, I'll look at doing it through the stored procedure as you mentioned.

                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