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. Database & SysAdmin
  3. Database
  4. Comparing 2 values in same column SQL (C#)

Comparing 2 values in same column SQL (C#)

Scheduled Pinned Locked Moved Database
databasecsharptutorial
5 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.
  • U Offline
    U Offline
    User 9875787
    wrote on last edited by
    #1

    Hi all. I am busy writing a program, in Microsoft Visual Web Developer, and have a database was several tables. The one I am focusing on now is the ACCOUNT table. This table contains username, password and email column. I want to compare a username entered into a textbox with the existing usernames in the ACCOUNT table, so that I can create a message saying that the entered user already exists. I know how to do this in C# with if statements, but am really unsure with SQL, since not sure how to use it with SELECT.Thanks

    L P M 3 Replies Last reply
    0
    • U User 9875787

      Hi all. I am busy writing a program, in Microsoft Visual Web Developer, and have a database was several tables. The one I am focusing on now is the ACCOUNT table. This table contains username, password and email column. I want to compare a username entered into a textbox with the existing usernames in the ACCOUNT table, so that I can create a message saying that the entered user already exists. I know how to do this in C# with if statements, but am really unsure with SQL, since not sure how to use it with SELECT.Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 9912091 wrote:

      I want to compare a username entered into a textbox with the existing usernames in the ACCOUNT table

      SELECT 1
      FROM Account
      WHERE Username = @Username

      Returns 1 of the username exists :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      U 1 Reply Last reply
      0
      • U User 9875787

        Hi all. I am busy writing a program, in Microsoft Visual Web Developer, and have a database was several tables. The one I am focusing on now is the ACCOUNT table. This table contains username, password and email column. I want to compare a username entered into a textbox with the existing usernames in the ACCOUNT table, so that I can create a message saying that the entered user already exists. I know how to do this in C# with if statements, but am really unsure with SQL, since not sure how to use it with SELECT.Thanks

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

        Yeah, what Eddy said. But, provided you have a unique index on the column (and you should), it may be more efficient simply to try the INSERT and catch the exception -- you have to do that anyway.

        1 Reply Last reply
        0
        • U User 9875787

          Hi all. I am busy writing a program, in Microsoft Visual Web Developer, and have a database was several tables. The one I am focusing on now is the ACCOUNT table. This table contains username, password and email column. I want to compare a username entered into a textbox with the existing usernames in the ACCOUNT table, so that I can create a message saying that the entered user already exists. I know how to do this in C# with if statements, but am really unsure with SQL, since not sure how to use it with SELECT.Thanks

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Instead of using 1 in Eddys query use count(*), this will tell you how many records match the name. You should study database design as you may run into some of the basic errors made by newbies. Primary keys and unique constraints.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • L Lost User

            Member 9912091 wrote:

            I want to compare a username entered into a textbox with the existing usernames in the ACCOUNT table

            SELECT 1
            FROM Account
            WHERE Username = @Username

            Returns 1 of the username exists :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            U Offline
            U Offline
            User 9875787
            wrote on last edited by
            #5

            Thanks! :)

            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