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.
  • N Nish Nishant

    Okay, but I just thought both of you (not just him) kinda went the "immature" route towards the end there.

    Regards, Nish


    My technology blog: voidnish.wordpress.com

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

    Fair enough. Regardless, this is a good discussion.


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

    N 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

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #26

      Welcome to the code project, what is your iternicene debate of choice today, sir? :-)

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-A Dead ringer for Kate Winslett[^]

      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;

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #27

        On a point of order, the exception mechanism isn't that expensive, except when compiled for debug. For live code it is similar to eventing see John Skeet's Blog: http://www.yoda.arachsys.com/csharp/exceptions.html[^].

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-A Dead ringer for Kate Winslett[^]

        G 1 Reply Last reply
        0
        • K Keith Barrow

          On a point of order, the exception mechanism isn't that expensive, except when compiled for debug. For live code it is similar to eventing see John Skeet's Blog: http://www.yoda.arachsys.com/csharp/exceptions.html[^].

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]
          -Or-A Dead ringer for Kate Winslett[^]

          G Offline
          G Offline
          Gary Wheeler
          wrote on last edited by
          #28

          I was talking out of my hat, there. Generally speaking, exception mechanisms tend to be more expensive than normal control flow. In the SQL environment, that difference may be inconsequential compared to everything else that's going on.

          Software Zen: delete this;

          K 1 Reply Last reply
          0
          • T T M Gray

            But do you know why it is bad practice? My point is that you shouldn't do something a certain way just because someone says so (even if that someone is Microsoft). Nothing in that article explains why you shouldn't use exceptions for program flow. If you had posted this[^] instead, that at least gives reasons related to performance. There should be real reasons behind why you code a certain way. If the original poster is not strong in SQL and has no method of source control for database schema then using the stored procedure solution makes it less maintainable. That probably outweighs the performance impact of the Exception use in one method of a small application. Blindly following a "best practice" without considering the specific situation is a bad idea. Consider this case[^] where it is a choice of one exception or 4 database roundtrips. Avoid dogma in code.

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

            There's no hard and fast rules. But I agree with Mark. If a known condition can be tested for without relying on an exception, it's a best course of action. You cannot reliably know the behavior of a system as that system and its dependants rely on a failure mode, especially when any parts of the system get upgraded in the future. What may be an exception in one version may NOT be in the next.

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

            A 1 Reply Last reply
            0
            • G Gary Wheeler

              I was talking out of my hat, there. Generally speaking, exception mechanisms tend to be more expensive than normal control flow. In the SQL environment, that difference may be inconsequential compared to everything else that's going on.

              Software Zen: delete this;

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #30

              Just being awkward really :-) As you say, whatever the code, the SQL hit is going to be the worst and swamp everything else. I agree with Mark's initial thoughts, you should avoid raising exceptions in the first place for the most part, but wouldn't vomit at the try/catch either ( I would avoid a re-throw though). The one thing I don't think should enter the equation is the performance (ironically, given my post), until it is known that it will adversely impact the system. Early optimisation wrecks good clean code. If there are problems, they are likely to involve a small proportion of the code, which should be optimised as fully as possible. That's my hapney'sworth anyway.

              Sort of a cross between Lawrence of Arabia and Dilbert.[^]
              -Or-A Dead ringer for Kate Winslett[^]

              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

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

                Well, this has certainly been an interesting debate. What nobody has seemed to point out so far is that you actually should combine the two techniques. This is for a simple reason - while you should certainly attempt to detect the unique key violation explicitly, there is no guarantee that this alone will work. The reason for this is simple, presumably your application is going to be multi-user; well, between the time you check the uniqueness and the actual insert occurs, another user could have inputted the same values. So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition. Simple. Job done.:thumbsup:

                I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                N A P 3 Replies Last reply
                0
                • P Pete OHanlon

                  Well, this has certainly been an interesting debate. What nobody has seemed to point out so far is that you actually should combine the two techniques. This is for a simple reason - while you should certainly attempt to detect the unique key violation explicitly, there is no guarantee that this alone will work. The reason for this is simple, presumably your application is going to be multi-user; well, between the time you check the uniqueness and the actual insert occurs, another user could have inputted the same values. So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition. Simple. Job done.:thumbsup:

                  I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Onyx

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

                  As I responded prior, I would use TryParse rather than Try/Catch but be prepared for UnhandledExceptions There is no one size fits all in software development and as the saying goes, they keep building better idiots.


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

                  A 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Well, this has certainly been an interesting debate. What nobody has seemed to point out so far is that you actually should combine the two techniques. This is for a simple reason - while you should certainly attempt to detect the unique key violation explicitly, there is no guarantee that this alone will work. The reason for this is simple, presumably your application is going to be multi-user; well, between the time you check the uniqueness and the actual insert occurs, another user could have inputted the same values. So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition. Simple. Job done.:thumbsup:

                    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #33

                    Pete O'Hanlon wrote:

                    So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition.

                    I wouldn't do both if the exception handling is going to be necessary anyway. I see no advantage to doing that (e.g., performance would probably be better the exception route anyway). I sometimes see this strategy when dealing with multithreaded applications (e.g., you check a condition that is fast and commonly results in false, then you lock, then you check that condition again). However, I don't think that technique applies here, so I'd just go with the exception handling.

                    [Forum Guidelines]

                    P 1 Reply Last reply
                    0
                    • N Not Active

                      As I responded prior, I would use TryParse rather than Try/Catch but be prepared for UnhandledExceptions There is no one size fits all in software development and as the saying goes, they keep building better idiots.


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

                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #34

                      What does TryParse have to do with attempting to insert a duplicate key?

                      [Forum Guidelines]

                      N 1 Reply Last reply
                      0
                      • A AspDotNetDev

                        Pete O'Hanlon wrote:

                        So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition.

                        I wouldn't do both if the exception handling is going to be necessary anyway. I see no advantage to doing that (e.g., performance would probably be better the exception route anyway). I sometimes see this strategy when dealing with multithreaded applications (e.g., you check a condition that is fast and commonly results in false, then you lock, then you check that condition again). However, I don't think that technique applies here, so I'd just go with the exception handling.

                        [Forum Guidelines]

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

                        The reason for the first is simply that you are giving the user information up front that the item already exists. The try/catch is for the really exceptional case where there's a race condition.

                        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                        Forgive your enemies - it messes with their heads

                        My blog | My articles | MoXAML PowerToys | Onyx

                        A 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          There's no hard and fast rules. But I agree with Mark. If a known condition can be tested for without relying on an exception, it's a best course of action. You cannot reliably know the behavior of a system as that system and its dependants rely on a failure mode, especially when any parts of the system get upgraded in the future. What may be an exception in one version may NOT be in the next.

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

                          A Offline
                          A Offline
                          AspDotNetDev
                          wrote on last edited by
                          #36

                          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 1 Reply Last reply
                          0
                          • T T M Gray

                            You could catch that particular exception. You could use a stored procedure to do the insert that checks first and returns a value that tells you what happened. A third option would be to query before you do the insert, but that is inefficient as you are making two trips to the database instead of just one.

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

                            Programming by error - using an error in the normal flow of an operation. This is using the error trap is incorrect.

                            Never underestimate the power of human stupidity RAH

                            1 Reply Last reply
                            0
                            • P Pete OHanlon

                              The reason for the first is simply that you are giving the user information up front that the item already exists. The try/catch is for the really exceptional case where there's a race condition.

                              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                              Forgive your enemies - it messes with their heads

                              My blog | My articles | MoXAML PowerToys | Onyx

                              A Offline
                              A Offline
                              AspDotNetDev
                              wrote on last edited by
                              #38

                              I'd say that depends on the code/application. If the distinction between it being a race condition and it being an old duplicate key is unimportant, then I'd go with exception handling. If the race condition should be handled differently, then it makes sense to do both.

                              [Forum Guidelines]

                              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

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

                                Your problem is not how to deal with the error, the problem is what is the CAUSE of the error. There is no excuse for primary key violation and it is up to the developer to make sure the exception cannot arise in the normal flow of the program.

                                erdinc27 wrote:

                                if the user tries to add an existing data it gives error like that

                                I suspect you are inserting instead of updating a record.

                                Never underestimate the power of human stupidity RAH

                                P L 2 Replies Last reply
                                0
                                • A AspDotNetDev

                                  What does TryParse have to do with attempting to insert a duplicate key?

                                  [Forum Guidelines]

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

                                  The statement is in regard to exception handling in general, which is what the debate is about, not a specific, narrow case of one type of SqlException


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

                                  P 1 Reply Last reply
                                  0
                                  • N Not Active

                                    The statement is in regard to exception handling in general, which is what the debate is about, not a specific, narrow case of one type of SqlException


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

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

                                    Ah, thanks for clearing that up. I was a touch confused on this.

                                    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
                                    • M Mycroft Holmes

                                      Your problem is not how to deal with the error, the problem is what is the CAUSE of the error. There is no excuse for primary key violation and it is up to the developer to make sure the exception cannot arise in the normal flow of the program.

                                      erdinc27 wrote:

                                      if the user tries to add an existing data it gives error like that

                                      I suspect you are inserting instead of updating a record.

                                      Never underestimate the power of human stupidity RAH

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

                                      10!

                                      1 Reply Last reply
                                      0
                                      • P Pete OHanlon

                                        Well, this has certainly been an interesting debate. What nobody has seemed to point out so far is that you actually should combine the two techniques. This is for a simple reason - while you should certainly attempt to detect the unique key violation explicitly, there is no guarantee that this alone will work. The reason for this is simple, presumably your application is going to be multi-user; well, between the time you check the uniqueness and the actual insert occurs, another user could have inputted the same values. So, you have two checks in there - one to cope with the initial check, and a try/catch to cope with the database race condition. Simple. Job done.:thumbsup:

                                        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                                        Forgive your enemies - it messes with their heads

                                        My blog | My articles | MoXAML PowerToys | Onyx

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

                                        Pete O'Hanlon wrote:

                                        there is no guarantee that this alone will work

                                        10!

                                        1 Reply Last reply
                                        0
                                        • N Not Active

                                          T M Gray wrote:

                                          Your statement is a matter of philosophy or preference.

                                          No, established best practices, architecture guidance and experience. http://msdn.microsoft.com/en-us/library/seyhszts.aspx[^] "Know when to set up a try/catch block. For example, you can programmatically check for a condition that is likely to occur without using exception handling. In other situations, using exception handling to catch an error condition is appropriate." In this case the unique key violation is a known condition that may occur 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
                                          #44

                                          Mark Nischalke wrote:

                                          In this case the unique key violation is a known condition

                                          Which the database will check anyway; so why check it twice or more? You're just slowing things down needlessly. Especially considering that the internal check by the database is likely to be the quickest.

                                          N 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