shahab Posted March 19, 2004 Posted March 19, 2004 When the record of database is unique and user adds a duplicated value in it, in error handling how can I notice the user. (I mean the error must have a id or sth like that..) } catch (Exception e01) { Label3.Text=e01.Message.ToString(); } :rolleyes: Thanks Quote
MorningZ Posted March 19, 2004 Posted March 19, 2004 if you do a Try Catch block, like Try 'some SQL insert statement Catch ex as Exception 'ex.Message has the error message.. you could just look for certain text here (depends on the database though) End Try Quote If you make it idiot proof, they'll build a better idiot :-)
Administrators PlausiblyDamp Posted March 19, 2004 Administrators Posted March 19, 2004 What database are you using? If it is SQL then you are much better trying to catch a SqlException - this will allow you to check the SQL error code. If you are using access then check for a OleDbException this at least gives you an ErrorCode property. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
shahab Posted March 20, 2004 Author Posted March 20, 2004 1-depends on the database though would you please show me a code? 2-I use msSql2000 If it is SQL then you are much better trying to catch a SqlException - this will allow you to check the SQL error code! I know the theory i need code!!!:) Quote
Administrators PlausiblyDamp Posted March 20, 2004 Administrators Posted March 20, 2004 something like the following should help try{ //whatever code you where doing } catch(SqlException ex) { if (ex.Number == 2627) //IIRC 2627 is a PK violation { //handle duplicate id here } } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
shahab Posted March 20, 2004 Author Posted March 20, 2004 oh Thanks like this: //Label6.Text = e01.Number.ToString(); if (e01.Number == 2627) { Label6.Text = "Sorry there is a username with the same what u typed!Please try new username"; } :) //handle duplicate id here if there were many errors what should I do then? Quote
Administrators PlausiblyDamp Posted March 20, 2004 Administrators Posted March 20, 2004 ex has an errors collection - you can use a foreach loop to loop over all the errors returned by SQL. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.