Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2004, 01:15 PM
Authorized User
 
Join Date: Dec 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Catch and display an appropriate error

I have a stored procedure like this.
---------------------------------
Procedure check_user(p_user_name in varchar2, userid in out integer)
   select userid, active_bln into v_user_id, v_active_bln from table where username = p_user_name ;
userid := v_user_id

if v_active_bln <> "Y" Then
raise_application_error(-20001,"user disabled")
end if


raise_application_error(-20004,"invaild username")
end Procedure
-------------------------------------
My .NET ode
--------------------------------------------------
Public Function CheckUser(username as integer)
Try
Conn.Open
cmd = conn.createcommad
cmd.commandtext = check_user
cmd.parameters.add("p_user_name", username)
cmd.parameters.add("p_user_id", oracledbtype.int32, parameterdirection.output)
cmd.excutenonquery()
return cmd.parameters("p_user_id").value
Catch ex as exception
Throw
End Try

-------------------------------------
How can I catch and display an appropriate error "Invalid username" to the user if the user name doesn't exist in the table.Also If the user is disabled the error message should be "User is disabled".

Thanks..
 
Old December 15th, 2004, 06:19 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It depends which connection object you are using (OleDb or Sql) but if it's OleDbConnection you can catch the OleDbException and check it's Errors collection. Each item in the Errors collection has a NativeError property this should store you're -20004 and -20001 error codes.

You can check that way.

Kep.
 
Old December 15th, 2004, 07:31 PM
Authorized User
 
Join Date: Jan 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rathbird
Default

I'm assuming you're using OleDb because you use an oracledbtype.int32 data type, so before you catch your general exceptions (Catch ex as exception) you should catch your OleDb exceptions:

Catch ex as System.Data.OleDb.OleDbException

Then you can test on ex.ErrorCode for the specific error (you have to do the leg work to find out the error number that is returned with a null recordset) and send your own message to the client:

If ex.ErrorCode = -20004 Then throw new ApplicationException("Username not found")

or the Oledb exception:

If ex.ErrorCode = -20004 Then throw new ApplicationException(ex.Message)

Now I generally write in C#, so my VB might be a little rusty, but I know that if you let your general exception handler catch database exceptions, you will lose the error code and therefore be unable to send the correct message to the user.

Hope this helps!

Robin


 
Old December 16th, 2004, 11:00 AM
Authorized User
 
Join Date: Dec 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your help.It's working fine.






Similar Threads
Thread Thread Starter Forum Replies Last Post
catch 404 error danielnixon ASP.NET 2.0 Basics 1 May 1st, 2008 06:57 PM
Unable to catch error HttpException (0x80004005): Shibu General .NET 6 May 22nd, 2007 04:21 AM
Catch PHP error Mantis Pro PHP 7 January 19th, 2005 01:05 PM
Catch POST error ? Mantis HTML Code Clinic 1 December 18th, 2004 06:37 PM
How to catch error. lancet2003 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 December 3rd, 2003 02:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.