SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Handling Errors in Stored Procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

burzum
Yak Posting Veteran

55 Posts

Posted - 05/22/2012 :  15:05:59  Show Profile  Reply with Quote
Greetings,

I have searched the forum for the particular problem I'm facing, but didn't find what I've been looking for.

I have a stored procedure:

USE [Dynamic_Glossary]
GO

/****** Object:  StoredProcedure [dbo].[Output_Data]    Script Date: 05/22/2012 09:47:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

alter procedure [dbo].[Output_Data]
(
	@ColumnNumber  sysname,
	@language varchar (30)
	
	
)
AS
SET NOCOUNT ON;



BEGIN
TRY

EXEC ('SELECT English_Words,  Word_Def as Definition, '+@language+' as "Translation To" FROM [Languages] WHERE [' + @ColumnNumber + '] = 1')




END TRY

BEGIN CATCH
PRINT 'Your Language is not supoorted'
end catch





And what I'm trying to do is to show a message "The current language is not supported"

I have 11 columns with different languages in a table, so what I want is if the input doesn't match the Column Name it shows that error message.

I just recently added the TRY and CATCH, it doesn't throw an error anymore, but it doesn't print the catch statement, it returns 0.

Edited by - burzum on 05/22/2012 15:19:59

Lamprey
Flowing Fount of Yak Knowledge

3829 Posts

Posted - 05/22/2012 :  15:32:39  Show Profile  Reply with Quote
I cannot repro you issue, maybe try adding this select to your catach block to see if it returns anything:
	
SELECT
      ERROR_NUMBER() AS ErrorNumber
      ,ERROR_SEVERITY() AS ErrorSeverity
      ,ERROR_STATE() AS ErrorState
      ,ERROR_PROCEDURE() AS ErrorProcedure
      ,ERROR_LINE() AS ErrorLine
      ,ERROR_MESSAGE() AS ErrorMessage;
It's possible that someting is generating an error severity that a catch block won't trap. Also, is there any chance you can change your schema to a more normalized one?

Edited by - Lamprey on 05/22/2012 15:33:12
Go to Top of Page

burzum
Yak Posting Veteran

55 Posts

Posted - 05/22/2012 :  15:44:23  Show Profile  Reply with Quote
Thank you for quick reply.

I have that actually in my code I was just testing it. The error I get is
ErrorMessage
Invalid column name 'sdf'.


So what I want is instead of "invalid column name" it prints a different message "Language is not supported.
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3829 Posts

Posted - 05/22/2012 :  17:13:31  Show Profile  Reply with Quote
I'm not sure how you are testing your code, but from SSMS I get a printed message in the Messages tab.
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3829 Posts

Posted - 05/22/2012 :  17:13:55  Show Profile  Reply with Quote
Perhaps you want to use the RAISERROR function?
Go to Top of Page

burzum
Yak Posting Veteran

55 Posts

Posted - 05/22/2012 :  17:43:38  Show Profile  Reply with Quote
I might explaining this wrong. Let me try this once more.

So when I execute the above procedure it runs perfectly. And I do get the message saying "Your Language is not supported". But that's in the Message tab. In the result tab, I get Return Value 0. What I want is so somehow that message showed in the result tab, because later I'm using this procedure in C#. And when I run my C# code and if the columnName doesn't match it shows a blank page. And instead of that I want to print "Language is not supported".
Go to Top of Page

burzum
Yak Posting Veteran

55 Posts

Posted - 05/24/2012 :  17:59:25  Show Profile  Reply with Quote
Any help?
Go to Top of Page

LoztInSpace
Aged Yak Warrior

876 Posts

Posted - 05/25/2012 :  03:02:35  Show Profile  Reply with Quote
I'd start again.
You should not need to use dynamic SQL to do a simple translation lookup. Redesign your database so you don't have to do that.
As Lamprey says - raise an actual error and deal with that in your client code.
If you really insist then you have to do
select 'The current language is not supported'

to get your message out in the results.

But you really want to look at the design - it's not correct.
Go to Top of Page

SQL Basic
Starting Member

Canada
8 Posts

Posted - 05/25/2012 :  05:43:59  Show Profile  Reply with Quote
Is there nothing script code that mistake in your structure?
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3829 Posts

Posted - 05/25/2012 :  11:33:22  Show Profile  Reply with Quote
quote:
Originally posted by burzum

I might explaining this wrong. Let me try this once more.

So when I execute the above procedure it runs perfectly. And I do get the message saying "Your Language is not supported". But that's in the Message tab. In the result tab, I get Return Value 0. What I want is so somehow that message showed in the result tab, because later I'm using this procedure in C#. And when I run my C# code and if the columnName doesn't match it shows a blank page. And instead of that I want to print "Language is not supported".

As LoztInSpace mentioned, just PRINTing from SQL will not do anything to your application or web page. You need to return the string either via a SELECT, and OUTPUT parameter or using RAISERROR (as an error or warning). Then deal with that result in your calling environment.
Go to Top of Page

burzum
Yak Posting Veteran

55 Posts

Posted - 05/30/2012 :  13:55:39  Show Profile  Reply with Quote
Thank you, that helped.

However, there's one problem I'm still struggling with.

What I want want is if the Column Name doesn't match it should print an output. So far it does that in SQL. But I'm using this stored procedure in C#. So now, when I run it and if the column name doesn't match it shows an error, instead of showing me that message. Any ideas on how to display that message in C#.

That is the error:

"Cannot find column [English_Words]."
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000