Solved

Proc error handling.  vb.net

Posted on 2014-12-11
2
202 Views
Last Modified: 2014-12-12
The following proc deletes records in tblSoftware. SoftwareID from this table is used as FK in another table. On delete, it will produce some errors ("There are some records with this SoftwareID in tblOrderDetails; cannot delete this item.").

What are options to show this error message.

Question: How to catch this error in try/catch with multiple catch for this error and some other error may happen?

ALTER PROCEDURE [dbo].[spSoftwareDelete]
     @SoftwareID int
As
BEGIN

	Delete From tblSoftware Where SoftwareID = @SoftwareID 
   
	if @@Error>0 
        Begin
		 -- Set @msg = @msg + '; SQL Server error: ' + CAST(@@Error AS NVARCHAR(10))
		-- Raise an error and return
		RAISERROR ('There are some records with this SoftwareID in tblOrderDetails; cannot delete this item..', 16, 1)
		RETURN @@ERROR
        End 
END 

Open in new window

0
Comment
Question by:Mike Eghtebas
2 Comments
 
LVL 11

Accepted Solution

by:
LordWabbit earned 500 total points
Comment Utility
Well it would be best to check for the FK restraint first, rather then just trying to delete and failing.  That way you can decide what message needs to be sent, as well as having a catch all for unexpected errors (deadlock's etc.)
Something like
ALTER PROCEDURE [dbo].[spSoftwareDelete]
     @SoftwareID int
As
BEGIN
	DECLARE @Check INT
	SET @Check = (SELECT COUNT(*) FROM OtherTable WHERE SoftwareID = @SoftwareID)
	IF (@Check > 0)
	BEGIN
		RAISERROR ('There are some records with this SoftwareID in tblOrderDetails; cannot delete this item..', 16, 1)
		RETURN @@ERROR
	END
	ELSE
	BEGIN
	Delete From tblSoftware Where SoftwareID = @SoftwareID 
   
	if @@Error>0 
        Begin
		 -- Set @msg = @msg + '; SQL Server error: ' + CAST(@@Error AS NVARCHAR(10))
		-- Raise an error and return
		RAISERROR ('There are some records with this SoftwareID in tblOrderDetails; cannot delete this item..', 16, 1)
		RETURN @@ERROR
        End 
END 

Open in new window

0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
Comment Utility
Thank you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now