473,218 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Handling of different return code correspondingly in a stored procedure

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);

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

void InsertScannedFile (int id)
{
this.ExecuteStoredProcedure(lReturn,
Constants.StoredProcedures.SPInsertScannedFile,
new SqlParameter("@ID",id));

return lReturn.SPInsertScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScannedFile
@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_inserted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.DistributionID = @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.Text as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Text as "The file is
already scanned.".

Thanks!

Dec 15 '06 #1
5 1706
Fir5tSight,

Normally you would use commandObject.ExecuteScalar. 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********@yahoo.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
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);

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

void InsertScannedFile (int id)
{
this.ExecuteStoredProcedure(lReturn,
Constants.StoredProcedures.SPInsertScannedFile,
new SqlParameter("@ID",id));

return lReturn.SPInsertScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScannedFile
@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_inserted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.DistributionID = @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.Text as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Text 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_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
try
{
Row lRow = this.Connection.InsertScannedFile(ID);

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

void InsertScannedFile (int id)
{
this.ExecuteStoredProcedure(lReturn,

Constants.StoredProcedures.SPInsertScannedFile,
new SqlParameter("@ID",id));

return lReturn.SPInsertScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScannedFile
@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_inserted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.DistributionID = @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.Text as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Text 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********@yahoo.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
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);

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

void InsertScannedFile (int id)
{
this.ExecuteStoredProcedure(lReturn,
Constants.StoredProcedures.SPInsertScannedFile,
new SqlParameter("@ID",id));

return lReturn.SPInsertScannedFile[0];
}

// The stored procedure is like the follows:

CREATE PROCEDURE SPInsertScannedFile
@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_inserted
)
SELECT
@ID,
getdate()

-- return the details
SELECT
-- some field names
FROM
table1 INNER JOIN table2 -- etc.
WHERE
table1.DistributionID = @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.Text as "The file is already in the
system!" However, if 2 is returnd, set textMessage.Text 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
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...
9
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...
0
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...
5
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...
9
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...
0
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
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...
1
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...
4
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.