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. Other Discussions
  3. The Weird and The Wonderful
  4. DataBase Selects and other lovely things :)

DataBase Selects and other lovely things :)

Scheduled Pinned Locked Moved The Weird and The Wonderful
databaseregex
9 Posts 7 Posters 21 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.
  • L Offline
    L Offline
    Luka Grabarevic
    wrote on last edited by
    #1

    Today I was looking through the code of student who is working on some project for our company. He's trying to make a new user admininstration application. In his application he is loading the users from the database in a treeview. Ok, so far so good. Nothing is wrong. Now I've tried to load the user informations and wondered why the data didn't match to the selected user. The following code snippet is the reason. string query = "ID = " + this.userTree.SelectedNode.Index.ToString(); DataRow[] foundi = tableAdapterUsers.Select(query); He's making a Select on the Database expecting that the TreeViewID is equal the Database ID. :doh: I spend the nearly the whole morning to find this. I'm wishing everbody a nice weekend greetings pdluke

    P N 2 Replies Last reply
    0
    • L Luka Grabarevic

      Today I was looking through the code of student who is working on some project for our company. He's trying to make a new user admininstration application. In his application he is loading the users from the database in a treeview. Ok, so far so good. Nothing is wrong. Now I've tried to load the user informations and wondered why the data didn't match to the selected user. The following code snippet is the reason. string query = "ID = " + this.userTree.SelectedNode.Index.ToString(); DataRow[] foundi = tableAdapterUsers.Select(query); He's making a Select on the Database expecting that the TreeViewID is equal the Database ID. :doh: I spend the nearly the whole morning to find this. I'm wishing everbody a nice weekend greetings pdluke

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

      Youch. When I build TreeNodes from DataRows I store the original DataRow in the Tag field of the TreeNode for future reference.

      1 Reply Last reply
      0
      • L Luka Grabarevic

        Today I was looking through the code of student who is working on some project for our company. He's trying to make a new user admininstration application. In his application he is loading the users from the database in a treeview. Ok, so far so good. Nothing is wrong. Now I've tried to load the user informations and wondered why the data didn't match to the selected user. The following code snippet is the reason. string query = "ID = " + this.userTree.SelectedNode.Index.ToString(); DataRow[] foundi = tableAdapterUsers.Select(query); He's making a Select on the Database expecting that the TreeViewID is equal the Database ID. :doh: I spend the nearly the whole morning to find this. I'm wishing everbody a nice weekend greetings pdluke

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        pdluke wrote:

        string query = "ID = " + this.userTree.SelectedNode.Index.ToString();

        It is interesting to see people make sql queries this way and expose themselves to SQL injections. Parameterized queries are not only safer, but also easier, IMHO.


        Programming Blog utf8-cpp

        P M 2 Replies Last reply
        0
        • N Nemanja Trifunovic

          pdluke wrote:

          string query = "ID = " + this.userTree.SelectedNode.Index.ToString();

          It is interesting to see people make sql queries this way and expose themselves to SQL injections. Parameterized queries are not only safer, but also easier, IMHO.


          Programming Blog utf8-cpp

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

          Nemanja Trifunovic wrote:

          Parameterized queries are not only safer, but also easier

          I'm not suggesting I agree with the method shown in by the OP, but it is much easier to keep a source history if the queries are in the code rather than stored procedures and also less likely to miss an update when releasing a new version of code. That said, i use parameterized queries:laugh: Pualee

          A N 2 Replies Last reply
          0
          • P Pualee

            Nemanja Trifunovic wrote:

            Parameterized queries are not only safer, but also easier

            I'm not suggesting I agree with the method shown in by the OP, but it is much easier to keep a source history if the queries are in the code rather than stored procedures and also less likely to miss an update when releasing a new version of code. That said, i use parameterized queries:laugh: Pualee

            A Offline
            A Offline
            Andy Brummer
            wrote on last edited by
            #5

            You don't have to use stored procedures to use parameterized queries, just put parameters in your queries. "ID = @id" instead of "ID = '" + id.ToString() + "'"


            Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

            1 Reply Last reply
            0
            • P Pualee

              Nemanja Trifunovic wrote:

              Parameterized queries are not only safer, but also easier

              I'm not suggesting I agree with the method shown in by the OP, but it is much easier to keep a source history if the queries are in the code rather than stored procedures and also less likely to miss an update when releasing a new version of code. That said, i use parameterized queries:laugh: Pualee

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              Pualee wrote:

              if the queries are in the code rather than stored procedures

              I never mentioned stored procedures :~ . Parameterized queries can be kept in the source code.


              Programming Blog utf8-cpp

              P 1 Reply Last reply
              0
              • N Nemanja Trifunovic

                Pualee wrote:

                if the queries are in the code rather than stored procedures

                I never mentioned stored procedures :~ . Parameterized queries can be kept in the source code.


                Programming Blog utf8-cpp

                P Offline
                P Offline
                Pualee
                wrote on last edited by
                #7

                For me, writing on this forum is always a tradeoff between looking stupid and learning more... Thanks for the clarification:|

                P 1 Reply Last reply
                0
                • P Pualee

                  For me, writing on this forum is always a tradeoff between looking stupid and learning more... Thanks for the clarification:|

                  P Offline
                  P Offline
                  Psycho Coder Extreme
                  wrote on last edited by
                  #8

                  We use Stored Procedures at the organization I work for, but they have to be created in VS (which is under Source Control) before they can be created on the test database (with each release we submit release notes with files we've modified, created or removed and the person responsible for the builds handles it from there) that way the stored procedures are also under source control.

                  "Okay, I give up: which is NOT a real programming language????" Michael Bergman

                  "Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch

                  "Let's face it, the average computer user has the brain of a Spider Monkey." Bill Gates

                  1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    pdluke wrote:

                    string query = "ID = " + this.userTree.SelectedNode.Index.ToString();

                    It is interesting to see people make sql queries this way and expose themselves to SQL injections. Parameterized queries are not only safer, but also easier, IMHO.


                    Programming Blog utf8-cpp

                    M Offline
                    M Offline
                    maz2331
                    wrote on last edited by
                    #9

                    Or.... just write a function to check all your input fields before processing them? Little things... like making sure numeric values are in "int" and strings are properly quoted out and escaped before concatenating the final SQL string works fine too. Just gotta be careful.

                    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