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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. handle sql error to stop user reputation

handle sql error to stop user reputation

Scheduled Pinned Locked Moved ASP.NET
databaseperformancehelpquestioncode-review
5 Posts 3 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.
  • N Offline
    N Offline
    netsooz Amir Hamidi
    wrote on last edited by
    #1

    hi I have this webpage called register.aspx. user has a form of 4 fields and completes them to get an activation email and ... . Where I want to optimize my code is a place where I want to use a try & catch block where if user enters an existing username he will divert to catch block that tells him to reconsider about his/her username. I mean I want to stop username duplication, what kind of exeption or sql query do I need to use? Please inform me with details since I am kind of new Thanks

    Proper Prepration Prevents Poor Performance! h

    C S 2 Replies Last reply
    0
    • N netsooz Amir Hamidi

      hi I have this webpage called register.aspx. user has a form of 4 fields and completes them to get an activation email and ... . Where I want to optimize my code is a place where I want to use a try & catch block where if user enters an existing username he will divert to catch block that tells him to reconsider about his/her username. I mean I want to stop username duplication, what kind of exeption or sql query do I need to use? Please inform me with details since I am kind of new Thanks

      Proper Prepration Prevents Poor Performance! h

      C Offline
      C Offline
      Curtis Schlak
      wrote on last edited by
      #2

      You can put a primary key or unique constraint on the user name column and then, when the SqlException gets thrown because of violation of the constraint, you can display the error message to the user.

      "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

      N 1 Reply Last reply
      0
      • C Curtis Schlak

        You can put a primary key or unique constraint on the user name column and then, when the SqlException gets thrown because of violation of the constraint, you can display the error message to the user.

        "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

        N Offline
        N Offline
        netsooz Amir Hamidi
        wrote on last edited by
        #3

        hi there, for sure this solution is the best but please tell me how should I do this. I have my primary key on ID field but how can I put unique constraint on a field? and what is the code to to get the thrown sqlexception? please give me coding way too.

        Proper Prepration Prevents Poor Performance! h

        C 1 Reply Last reply
        0
        • N netsooz Amir Hamidi

          hi there, for sure this solution is the best but please tell me how should I do this. I have my primary key on ID field but how can I put unique constraint on a field? and what is the code to to get the thrown sqlexception? please give me coding way too.

          Proper Prepration Prevents Poor Performance! h

          C Offline
          C Offline
          Curtis Schlak
          wrote on last edited by
          #4

          A primary key, by definition, has a unique constraint on the column. The code should look something like this:

          try
          {
          ... try to insert new record ...
          }
          catch(SqlException se)
          {
          ... set error message to something appropriate ...
          }

          If you go to Google and search on "asp.net simple login page," you'll be amazed at the number of different samples for different databases that exist on the Internet. I don't feel like reproducing that here.

          "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

          1 Reply Last reply
          0
          • N netsooz Amir Hamidi

            hi I have this webpage called register.aspx. user has a form of 4 fields and completes them to get an activation email and ... . Where I want to optimize my code is a place where I want to use a try & catch block where if user enters an existing username he will divert to catch block that tells him to reconsider about his/her username. I mean I want to stop username duplication, what kind of exeption or sql query do I need to use? Please inform me with details since I am kind of new Thanks

            Proper Prepration Prevents Poor Performance! h

            S Offline
            S Offline
            Sam Horne
            wrote on last edited by
            #5

            Instead of relying on exceptions to catch the error, you should just do a database connection yourself selecting the user with the supplied username, if a row is returned, then that username exists. Following code is in VB.NET

            Dim strQuery as string = ""
            strQuery should be where you select * from users where username = username you're trying to test
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("mainDBConnection").ConnectionString)
            conn.Open()
            Dim cmd As New SqlCommand(conn.Database & ".dbo." & strQuery, conn)
            Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            if dr.HasRows = true then
            'DISPLAY ERROR MSG
            end if

            Obviously you'll have to change around specifics like database connection string etc. Hope this helps.

            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