473,832 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling of different return code correspondingly in a stored procedure

Hi All,

I have a C#.NET code as follows:

private void ScanInput_KeyPr ess(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
try
{
Row lRow = this.Connection .InsertScannedF ile(ID);

}
catch (Exception /*lException*/)
{
textMessage.Tex t = "The ID value cannot be found in the system.";
}
}

void InsertScannedFi le (int id)
{
this.ExecuteSto redProcedure(lR eturn,
Constants.Store dProcedures.SPI nsertScannedFil e,
new SqlParameter("@ ID",id));

return lReturn.SPInser tScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScanned File
@ID DTObjectID,

AS

IF EXISTS
(
SELECT
DistributionID
FROM
table1
WHERE
DistributionID = @ID

)
BEGIN
RAISERROR ('The file is already in the system!',16,1)
RETURN 1
END
IF NOT EXISTS
(
SELECT
FileID
FROM
table2
WHERE
FileID = @ID

)
BEGIN
RAISERROR ('The file is already scanned.',16,1)
RETURN 2
END
-- insert a new record
INSERT INTO
table2
(
ID,
Time_recorde_in serted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.Distribu tionID = @ID
RETURN 0
GO

--------------------------------------------------------------------------------
Could anyone advise me how to handle different return code (1 or 2)
differently from the stored procedure in the C# code? For instance, if
1 is returned, set textMessage.Tex t as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Tex t as "The file is
already scanned.".

Thanks!

Dec 15 '06 #1
5 1745
Fir5tSight,

Normally you would use commandObject.E xecuteScalar. Since you have a SELECT
returning - which is a recordset/resultset if you will - you will need to
navigate thru the results. If you will need to determine whether the result
set you're reading has more than one column. If it has just one column, that
it is your return result. Otherwise, is the resultset your are returning to
your interface. There is a property called FieldCount which would answer
that question for you.

Hope it helps.

Robson

"Fir5tSight " <fi********@yah oo.comwrote in message
news:11******** **************@ f1g2000cwa.goog legroups.com...
Hi All,

I have a C#.NET code as follows:

private void ScanInput_KeyPr ess(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
try
{
Row lRow = this.Connection .InsertScannedF ile(ID);

}
catch (Exception /*lException*/)
{
textMessage.Tex t = "The ID value cannot be found in the system.";
}
}

void InsertScannedFi le (int id)
{
this.ExecuteSto redProcedure(lR eturn,
Constants.Store dProcedures.SPI nsertScannedFil e,
new SqlParameter("@ ID",id));

return lReturn.SPInser tScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScanned File
@ID DTObjectID,

AS

IF EXISTS
(
SELECT
DistributionID
FROM
table1
WHERE
DistributionID = @ID

)
BEGIN
RAISERROR ('The file is already in the system!',16,1)
RETURN 1
END
IF NOT EXISTS
(
SELECT
FileID
FROM
table2
WHERE
FileID = @ID

)
BEGIN
RAISERROR ('The file is already scanned.',16,1)
RETURN 2
END
-- insert a new record
INSERT INTO
table2
(
ID,
Time_recorde_in serted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.Distribu tionID = @ID
RETURN 0
GO

--------------------------------------------------------------------------------
Could anyone advise me how to handle different return code (1 or 2)
differently from the stored procedure in the C# code? For instance, if
1 is returned, set textMessage.Tex t as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Tex t as "The file is
already scanned.".

Thanks!

Dec 15 '06 #2
Hi Robson,

Thanks for answering my question!

I want to know how to pass the specific error raised in the stored
procedure to the C# calling method. For instance, how to get the C#
code to display the message, 'The file is already in the system!', when
the first check in the stored procedure fails. For another instance,
how to get the C# code to display the message, 'The file is already
scanned.', when the second check in the stored procedure fails.

I use a return code of 1 and 2 to distinguish these two different
errors raised in the stored procedure. However, I wish to know how to
get this info in the calling C# code.

-Emily

Dec 16 '06 #3
On 15 Dec 2006 09:00:30 -0800, Fir5tSight wrote:
Hi All,

I have a C#.NET code as follows:

private void ScanInput_KeyPr ess(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
try
{
Row lRow = this.Connection .InsertScannedF ile(ID);

}
catch (Exception /*lException*/)
{
textMessage.Tex t = "The ID value cannot be found in the system.";
}
}

void InsertScannedFi le (int id)
{
this.ExecuteSto redProcedure(lR eturn,

Constants.Store dProcedures.SPI nsertScannedFil e,
new SqlParameter("@ ID",id));

return lReturn.SPInser tScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScanned File
@ID DTObjectID,

AS

IF EXISTS
(
SELECT
DistributionID
FROM
table1
WHERE
DistributionID = @ID

)
BEGIN
RAISERROR ('The file is already in the system!',16,1)
RETURN 1
END

IF NOT EXISTS
(
SELECT
FileID
FROM
table2
WHERE
FileID = @ID

)
BEGIN
RAISERROR ('The file is already scanned.',16,1)
RETURN 2
END

-- insert a new record
INSERT INTO
table2
(
ID,
Time_recorde_in serted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.Distribu tionID = @ID

RETURN 0
GO

--------------------------------------------------------------------------------
Could anyone advise me how to handle different return code (1 or 2)
differently from the stored procedure in the C# code? For instance, if
1 is returned, set textMessage.Tex t as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Tex t as "The file is
already scanned.".

Thanks!
Personally i'd eschew the return values approach and rewrite the procedure
to use output parameters instead. For one thing this would make extending
the logic easier as well as keeping the application logic in the
application.

So after executing the query i'd check the return value and then based on
that throw the appropriate exception within the .NET application
--
Bits.Bytes
http://bytes.thinkersroom.com
Dec 16 '06 #4
You shouldn't be sending back error message text from the stored procedure -
send back a numeric return code, put a corresponding enumeration in your
application code and throw an exception if appropriate. As was mentioned in
another reply, use output parameters instead of "return codes". In general,
you should always avoid putting application logic in the data layer.
Further, you should avoid passing back string values to indicate what are
really application errors. In other words, nothing "went wrong" in the data
layer; rather you received a result that indicates something "went wrong" in
the application.

"Fir5tSight " <fi********@yah oo.comwrote in message
news:11******** **************@ f1g2000cwa.goog legroups.com...
Hi All,

I have a C#.NET code as follows:

private void ScanInput_KeyPr ess(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
try
{
Row lRow = this.Connection .InsertScannedF ile(ID);

}
catch (Exception /*lException*/)
{
textMessage.Tex t = "The ID value cannot be found in the system.";
}
}

void InsertScannedFi le (int id)
{
this.ExecuteSto redProcedure(lR eturn,
Constants.Store dProcedures.SPI nsertScannedFil e,
new SqlParameter("@ ID",id));

return lReturn.SPInser tScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScanned File
@ID DTObjectID,

AS

IF EXISTS
(
SELECT
DistributionID
FROM
table1
WHERE
DistributionID = @ID

)
BEGIN
RAISERROR ('The file is already in the system!',16,1)
RETURN 1
END
IF NOT EXISTS
(
SELECT
FileID
FROM
table2
WHERE
FileID = @ID

)
BEGIN
RAISERROR ('The file is already scanned.',16,1)
RETURN 2
END
-- insert a new record
INSERT INTO
table2
(
ID,
Time_recorde_in serted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.Distribu tionID = @ID
RETURN 0
GO

--------------------------------------------------------------------------------
Could anyone advise me how to handle different return code (1 or 2)
differently from the stored procedure in the C# code? For instance, if
1 is returned, set textMessage.Tex t as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Tex t as "The file is
already scanned.".

Thanks!

Dec 17 '06 #5
Hi Karch,

Thanks for the advice! I'll use a parameter to return the error code
from the stored procedure, and then handle the exception based on the
error code.

I won't return any textual string from the stored procedure.

-Emily

Dec 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5130
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
9
10303
by: dtwilliams | last post by:
OK, i'm trying to do some error checking on stored procedures and am following the advise in Erland Sommarskog's 'Implementing Error Handling with Stored Procedures' document. Can anybody help with my stored procedures and why it keeps erroring at the '-- Create new Address Detail stage'? The errorCode value that is being return in my web app is 0, so i'm not even sure why it's even raising the error!! Rather than executing the INSERT...
0
4281
by: Rhino | last post by:
I've written several Java stored procedures now (DB2 V7.2) and I'd like to write down a few "best practices" for reference so that I will have them handy for future development. Would the experts here agree with the following? Would they add any other points? 1. If the shop standard calls for logging of application errors, a stored procedure should log any error that it encounters immediately upon detecting it and then return to the...
5
2283
by: Jurgen Defurne | last post by:
I am currently designing an application which should be accessible from different interfaces. For this I like to be using stored procedures to process the contents of form submissions and dialog screens. After studying the PG database, it seems to me that I can fulfill the following requirements. a) The basic contents of the internal data dictionary can be used to check incoming fields from on their length and permitted contents.
9
2701
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio and run it once you've made sure there is no harmful code. Currently we have several stored procedures which final result is a select with several joins that returns many
0
974
by: Fir5tSight | last post by:
Hi All, I have a C#.NET code as follows: private void ScanInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { try { Row lRow = this.Connection.InsertScannedFile(ID);
1
2257
by: pob | last post by:
>From a form I have some code that calls 4 modules frmMain 1 mod 2 mod 3 mod 4 mod If mod 1 experiences an error the error handling works fine within mod 1 and writes out the error to a table, but the other modules still get
1
2493
by: | last post by:
I have an application that has a presentation later, business layer, and data layer. All three projects have their own exception policy, the "UI Policy", "BL Policy", "DL Policy", all of which will log the error in the application event logs. When a database error occurs such as a missing stored procedure, all three policies will log the event resulting in three different entries in the error. My boss says there is a way in the...
4
5079
by: barmatt80 | last post by:
I am stumped on the error reporting with sql server. I was told i need to return @SQLCode(code showing if successful or not) and @ErrMsg(and the message returned). I am clueless on this. I wrote this procedure: ALTER PROCEDURE . @Emp_SSN int, @Annual_Forward decimal(10,2),
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10780
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10497
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4420
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.