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. giving error message according to Sql data

giving error message according to Sql data

Scheduled Pinned Locked Moved C#
databasecomhelp
63 Posts 14 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.
  • L Lost User

    Mycroft Holmes wrote:

    There is no excuse for primary key violation

    You're right in that - but the OP didn't have a primary key violation he had a unique constraint violation - which is legitimate. For example, I am adding new products each of which has a unique id (maybe a GUID or an integer) which will be the primary key. But the product also requires a human-readable Code - which is also required to be unique. At some point the user needs to type in the code for a new product - you can check the DB at this point, and tell them to go ahead. Meanwhile another user can try to use the same code, pass the 'does it exist' test, and attempt a commit. Whoever gets there first will be fine, the 2nd person's commit will fail.

    ___________________________________________ .\\axxx (That's an 'M')

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

    _Maxxx_ wrote:

    he had a unique constraint violation

    Ok I missed that mainly b/c I make no distinction between the 2 constraints, if that scenario existed then I would still do a select on the code field before inserting and feed the result back to the user. In the case of your new product I would return the record/info on the product entered by the first commit. I admit there is a potential for a conflict on a really high volume system but I have yet to meet such a system.

    Never underestimate the power of human stupidity RAH

    L 1 Reply Last reply
    0
    • N Not Active

      Not slowing it down. If you can't account for it otherwise, such as not applying an unique index on a field that may not be unique, then certainly the processing can, and should, be done at the database. If the key already exists return something like false not just let the exception be thrown.


      I know the language. I've read a book. - _Madmatt

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

      Mark Nischalke wrote:

      Not slowing it down.

      Yes, slowing it down. <Whoops, I hit post prematurely> GUI.DoesTheKeyAlreadyExist? BL.DoesTheKeyAlreadyExist? API.DoesTheKeyAlreadyExist? DAL.DoesTheKeyAlreadyExist? SP.DoesTheKeyAlreadyExist? DB.DoesTheKeyAlreadyExist? How many times are you going to check the same darn thing when no problem exists? A duplicate key is an exceptional situation, whether you can test it or not. Consider database round-trips to be very expensive. </Whoops, I hit post prematurely>

      Mark Nischalke wrote:

      If the key already exists return something like false

      How do I know false means duplicate rather than timeout or a referential integrity violation?

      N 1 Reply Last reply
      0
      • G Gary Wheeler

        I believe Mark's argument is that, given that the described error condition is a likely occurrence, then the OP should code to detect and handle the condition directly, rather than rely on the exception mechanism (which is expensive).

        Software Zen: delete this;

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

        Gary Wheeler wrote:

        the described error condition is a likely occurrence

        No, it never happens; I've certainly never seen one outside of unit testing -- of exception handling.

        N 1 Reply Last reply
        0
        • N Not Active

          I have always been taught and follow the principle that conditions that can be tested for are not exceptions and you shouldn't use exception handling for them. Certainly there are cases you can't test for or don't expect to happen. In this particular case the unique key violation is expected and can be tested for.


          I know the language. I've read a book. - _Madmatt

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

          Mark Nischalke wrote:

          conditions that can be tested for

          Do you check your car's battery charge, tire pressure, tread depth, lug nut torque, etc. every time you leave the house? Do you check your fly before leaving the men's room? Do you wear a belt and suspenders? Just being able to perform a test, doesn't make it worthwhile.

          N 1 Reply Last reply
          0
          • P PIEBALDconsult

            Mark Nischalke wrote:

            Not slowing it down.

            Yes, slowing it down. <Whoops, I hit post prematurely> GUI.DoesTheKeyAlreadyExist? BL.DoesTheKeyAlreadyExist? API.DoesTheKeyAlreadyExist? DAL.DoesTheKeyAlreadyExist? SP.DoesTheKeyAlreadyExist? DB.DoesTheKeyAlreadyExist? How many times are you going to check the same darn thing when no problem exists? A duplicate key is an exceptional situation, whether you can test it or not. Consider database round-trips to be very expensive. </Whoops, I hit post prematurely>

            Mark Nischalke wrote:

            If the key already exists return something like false

            How do I know false means duplicate rather than timeout or a referential integrity violation?

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #53

            Please, give me some credit. :rolleyes: Of course I would not architect such a contrived situation as you have outlined. Neither would I expect false to mean everything. As I, and others, have been saying here, handle the known conditions but be prepared for other cases.


            I know the language. I've read a book. - _Madmatt

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Gary Wheeler wrote:

              the described error condition is a likely occurrence

              No, it never happens; I've certainly never seen one outside of unit testing -- of exception handling.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #54

              Exactly the point. The application should be designed and coded so that it is an exceptional situation when it occurs. When responding to OP the first option given was to allow the exception to occur, which, IMO, is not correct, it should have been emphasized to correct the problem before it occurs or take other mitigating actions before just accepting the exception.


              I know the language. I've read a book. - _Madmatt

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Mark Nischalke wrote:

                conditions that can be tested for

                Do you check your car's battery charge, tire pressure, tread depth, lug nut torque, etc. every time you leave the house? Do you check your fly before leaving the men's room? Do you wear a belt and suspenders? Just being able to perform a test, doesn't make it worthwhile.

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #55

                If I expected my nuts to be loose I certainly would check my fly.


                I know the language. I've read a book. - _Madmatt

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  _Maxxx_ wrote:

                  he had a unique constraint violation

                  Ok I missed that mainly b/c I make no distinction between the 2 constraints, if that scenario existed then I would still do a select on the code field before inserting and feed the result back to the user. In the case of your new product I would return the record/info on the product entered by the first commit. I admit there is a potential for a conflict on a really high volume system but I have yet to meet such a system.

                  Never underestimate the power of human stupidity RAH

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

                  Mycroft Holmes wrote:

                  I admit there is a potential for a conflict on a really high volume system but I have yet to meet such a system.

                  Welcome to my world! Imagine, if you will, something like a public facing web site that requires registration using a user name (rather than an email address which reduces but doesn't eliminate the problem).

                  ___________________________________________ .\\axxx (That's an 'M')

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    Mycroft Holmes wrote:

                    I admit there is a potential for a conflict on a really high volume system but I have yet to meet such a system.

                    Welcome to my world! Imagine, if you will, something like a public facing web site that requires registration using a user name (rather than an email address which reduces but doesn't eliminate the problem).

                    ___________________________________________ .\\axxx (That's an 'M')

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

                    I can just about see the latency issue getting into it there, I would think even that would need to be a fairly high volume system. Have you ever tracked the number of constraint violations? I think I'll stick to my corporate systems and stay hidden behind the firewall thank you!

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Catch it and interpret it -- which database engine?

                      E Offline
                      E Offline
                      Erdinc27
                      wrote on last edited by
                      #58

                      hey guys thanks for all your replies i use MsSql 2008 express..and i tried something like that

                      try
                      {
                      dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
                      }
                      catch(SqlException ex)
                      {
                      Console.WriteLine("This Record is already exist");
                      }

                      but still on the screen it shows the same error which is shown in Sql screen and sometimes it gives error like object reference not set to an instance of an object in that line dr[j] = xnl.Item(i).ChildNodes[j].InnerText; what is wrong here.i read a Xml file over website and put it in a datatable is it because it cannot connect that site ?

                      vemedya.com

                      modified on Thursday, December 9, 2010 4:20 AM

                      P 1 Reply Last reply
                      0
                      • E Erdinc27

                        hey guys..i added a Unique constraint to a column in my sql table and if the user tries to add an existing data it gives error like that "Violation of UNIQUE KEY constraint 'ukc_cekilis_no'. Cannot insert duplicate key in object 'dbo.NumaraBilgileri'." it is ok..but i want to show an error message like that in my program..how i can check if the data already exist

                        vemedya.com

                        H Offline
                        H Offline
                        Hiren solanki
                        wrote on last edited by
                        #59

                        I think exception try..catch.. can only save you from this trouble. Just use Try and Catch SqlException. And check the detail in SqlException,If it Contains word 'UNIQUE' then you'll get sure that the exception is regarding UNIQUE Key Constrain and further more you can notify to users with a formatted message. That's not a big deal.

                        Regards, Hiren. Microsoft Dynamics CRM My Recent Article: - Way to know which control have raised PostBack[^]

                        P 1 Reply Last reply
                        0
                        • H Hiren solanki

                          I think exception try..catch.. can only save you from this trouble. Just use Try and Catch SqlException. And check the detail in SqlException,If it Contains word 'UNIQUE' then you'll get sure that the exception is regarding UNIQUE Key Constrain and further more you can notify to users with a formatted message. That's not a big deal.

                          Regards, Hiren. Microsoft Dynamics CRM My Recent Article: - Way to know which control have raised PostBack[^]

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

                          Hiren Solanki wrote:

                          ,If it Contains word 'UNIQUE'

                          And what do you do when you deploy it onto a localised version of SQL Server, say the Japanese version?

                          I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                          Forgive your enemies - it messes with their heads

                          My blog | My articles | MoXAML PowerToys | Onyx

                          1 Reply Last reply
                          0
                          • A AspDotNetDev

                            Dave Kreskowiak wrote:

                            If a known condition can be tested for without relying on an exception, it's a best course of action.

                            Well, I wouldn't say that's always true. Imagine if TryParse didn't exist. Would you rather Try/Catch a Parse or re-implement a version of parse that returns a bool if the string is invalid? Given the complexity of number formats (e.g., scientific notation), I would just do a Try/Catch. I guess this can be generalized as: If a known condition is exceptionally complex to test for, then just use exception handling to test if it's valid. Here's another example. I built a tool that allows the user to enter a regular expression to match against some data. Rather than validate that the regular expression is valid, I just used Try/Catch to catch exceptions thrown by invalid exceptions. I mean I COULD first check if the regular expression is valid, but it would be exceedingly complex and a waste of time when the test for validity already exists in the form of exception handling.

                            [Forum Guidelines]

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #61

                            aspdotnetdev wrote:

                            Imagine if TryParse didn't exist.

                            It didn't at one point. I said if it could be tested for. Obviously, you can't test for every possible failure in a Parse method. It's not practical since there are just too many possibilities. But, you can test for a single known condition, like a duplicate key.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            1 Reply Last reply
                            0
                            • N Not Active

                              Fair enough. Regardless, this is a good discussion.


                              I know the language. I've read a book. - _Madmatt

                              N Offline
                              N Offline
                              Nish Nishant
                              wrote on last edited by
                              #62

                              Mark Nischalke wrote:

                              Regardless, this is a good discussion.

                              Oh yes, in fact I wish we had more of these in the Lounge :-)

                              Regards, Nish


                              My technology blog: voidnish.wordpress.com

                              1 Reply Last reply
                              0
                              • E Erdinc27

                                hey guys thanks for all your replies i use MsSql 2008 express..and i tried something like that

                                try
                                {
                                dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
                                }
                                catch(SqlException ex)
                                {
                                Console.WriteLine("This Record is already exist");
                                }

                                but still on the screen it shows the same error which is shown in Sql screen and sometimes it gives error like object reference not set to an instance of an object in that line dr[j] = xnl.Item(i).ChildNodes[j].InnerText; what is wrong here.i read a Xml file over website and put it in a datatable is it because it cannot connect that site ?

                                vemedya.com

                                modified on Thursday, December 9, 2010 4:20 AM

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

                                In this case, I'd suspect that xml.Item(i) doesn't have any child nodes, so attempting to do anything with it would cause a problem.

                                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                                Forgive your enemies - it messes with their heads

                                My blog | My articles | MoXAML PowerToys | Onyx

                                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