473,654 Members | 3,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored procedure parameter output value

I'm calling a stored procedure which has an output parameter of type int.
Once the stored procedure is executed, I want to check the value of the
parameter in case it is null. However, when the a null value is returned I
don't seem to be able to detect it.

Any help would be greatly appreciated.

C# code is as follows:

SqlCommand cmd = new SqlCommand("sp_ GetApplicationI D", conn);
cmd.CommandType = CommandType.Sto redProcedure;

SqlParameter param = cmd.Parameters. Add("@iApplicat ionID",
SqlDbType.Int);
param.Direction = ParameterDirect ion.Output;

param = cmd.Parameters. Add("@vcApplica tionConstName",
SqlDbType.VarCh ar);
param.Value = sAppConstName;

if (conn.State == ConnectionState .Closed)
conn.Open();

cmd.ExecuteNonQ uery();
conn.Close();

// check for null here
if (cmd.Parameters["@iApplicationI D"].Value != null){
iID = (int)cmd.Parame ters["@iApplicationI D"].Value;
}
else{
throw new ApplicationExce ption("Unable to retrieve Application ID for
application: " + sAppConstName);
}

Stored Procedure (for test purposes):

CREATE PROCEDURE [dbo].[sp_BSQ_GetAppli cationID]

@vcApplicationC onstName varchar(50),
@iApplicationID int OUTPUT

AS

SET @iApplicationID = null

GO

Thanks in advance for your help.

Steven
Jul 21 '05 #1
4 2182
Hello

Compare with DBNull.Value instead of null.

if (cmd.Parameters["@iApplicationI D"].Value != DBNull.Value)
{
.....
}
Best regards,
Sherif
"Steven" <st************ @virgin.net> wrote in message
news:bo******** ***********@new s.demon.co.uk.. .
I'm calling a stored procedure which has an output parameter of type int.
Once the stored procedure is executed, I want to check the value of the
parameter in case it is null. However, when the a null value is returned I
don't seem to be able to detect it.

Any help would be greatly appreciated.

C# code is as follows:

SqlCommand cmd = new SqlCommand("sp_ GetApplicationI D", conn);
cmd.CommandType = CommandType.Sto redProcedure;

SqlParameter param = cmd.Parameters. Add("@iApplicat ionID",
SqlDbType.Int);
param.Direction = ParameterDirect ion.Output;

param = cmd.Parameters. Add("@vcApplica tionConstName",
SqlDbType.VarCh ar);
param.Value = sAppConstName;

if (conn.State == ConnectionState .Closed)
conn.Open();

cmd.ExecuteNonQ uery();
conn.Close();

// check for null here
if (cmd.Parameters["@iApplicationI D"].Value != null){
iID = (int)cmd.Parame ters["@iApplicationI D"].Value;
}
else{
throw new ApplicationExce ption("Unable to retrieve Application ID for
application: " + sAppConstName);
}

Stored Procedure (for test purposes):

CREATE PROCEDURE [dbo].[sp_BSQ_GetAppli cationID]

@vcApplicationC onstName varchar(50),
@iApplicationID int OUTPUT

AS

SET @iApplicationID = null

GO

Thanks in advance for your help.

Steven

Jul 21 '05 #2
Steven wrote:
I'm calling a stored procedure which has an output parameter of type int.
Once the stored procedure is executed, I want to check the value of the
parameter in case it is null. However, when the a null value is returned I
don't seem to be able to detect it.

Any help would be greatly appreciated.

C# code is as follows:

SqlCommand cmd = new SqlCommand("sp_ GetApplicationI D", conn);
cmd.CommandType = CommandType.Sto redProcedure;

SqlParameter param = cmd.Parameters. Add("@iApplicat ionID",
SqlDbType.Int);
param.Direction = ParameterDirect ion.Output;

param = cmd.Parameters. Add("@vcApplica tionConstName",
SqlDbType.VarCh ar);
param.Value = sAppConstName;

if (conn.State == ConnectionState .Closed)
conn.Open();

cmd.ExecuteNonQ uery();
conn.Close();

// check for null here
if (cmd.Parameters["@iApplicationI D"].Value != null){
iID = (int)cmd.Parame ters["@iApplicationI D"].Value;
}
else{
throw new ApplicationExce ption("Unable to retrieve Application ID for
application: " + sAppConstName);
}

Stored Procedure (for test purposes):

CREATE PROCEDURE [dbo].[sp_BSQ_GetAppli cationID]

@vcApplicationC onstName varchar(50),
@iApplicationID int OUTPUT

AS

SET @iApplicationID = null

GO

Thanks in advance for your help.

Steven


MSDN says:
When sending a null parameter value to the server, the user must specify
DBNull, not null. The null value in the system is an empty object that
has no value. DBNull is used to represent null values

So try
cmd.Parameters["@iApplicationI D"].Value != DBNull.Value

Dmitry

Jul 21 '05 #3
Excellent. Thanks.
Jul 21 '05 #4
oj
"NULLl" is a valid value and it's not the same as "null" - the literal that
represents a null reference.

Here is some info on DBNull class.

http://msdn.microsoft.com/library/de...ClassTopic.asp
"Wilford Munley" <tr****@tinkert oys.net> wrote in message
news:s5******** ************@ad elphia.com...
"Sherif ElMetainy" <el************ *@wayout.net.NO SPAM> wrote in message

news:u9******** ******@TK2MSFTN GP09.phx.gbl...
Hello

Compare with DBNull.Value instead of null.

if (cmd.Parameters["@iApplicationI D"].Value != DBNull.Value)
{
....
}
Best regards,
Sherif
"Steven" <st************ @virgin.net> wrote in message
news:bo******** ***********@new s.demon.co.uk.. .
I'm calling a stored procedure which has an output parameter of type int. Once the stored procedure is executed, I want to check the value of the parameter in case it is null. However, when the a null value is returned I don't seem to be able to detect it.

Any help would be greatly appreciated.

C# code is as follows:

SqlCommand cmd = new SqlCommand("sp_ GetApplicationI D", conn);
cmd.CommandType = CommandType.Sto redProcedure;

SqlParameter param = cmd.Parameters. Add("@iApplicat ionID",
SqlDbType.Int);
param.Direction = ParameterDirect ion.Output;

param = cmd.Parameters. Add("@vcApplica tionConstName",
SqlDbType.VarCh ar);
param.Value = sAppConstName;

if (conn.State == ConnectionState .Closed)
conn.Open();

cmd.ExecuteNonQ uery();
conn.Close();

// check for null here
if (cmd.Parameters["@iApplicationI D"].Value != null){
iID = (int)cmd.Parame ters["@iApplicationI D"].Value;
}
else{
throw new ApplicationExce ption("Unable to retrieve Application ID for application: " + sAppConstName);
}

Stored Procedure (for test purposes):

CREATE PROCEDURE [dbo].[sp_BSQ_GetAppli cationID]

@vcApplicationC onstName varchar(50),
@iApplicationID int OUTPUT

AS

SET @iApplicationID = null

GO

Thanks in advance for your help.

Steven



Why are DBNull and null different? Aren't they the same thing?

Wilford

Jul 21 '05 #5

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

Similar topics

7
9004
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in learning how to pass these varibables from ASP to the SP.
0
6693
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
4
19659
by: Steven | last post by:
I'm calling a stored procedure which has an output parameter of type int. Once the stored procedure is executed, I want to check the value of the parameter in case it is null. However, when the a null value is returned I don't seem to be able to detect it. Any help would be greatly appreciated. C# code is as follows: SqlCommand cmd = new SqlCommand("sp_GetApplicationID", conn);
2
5450
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
3
1764
by: Bilbo | last post by:
I have a a headscratcher here: I have a form that when submitted should do 2 things when a user enters data and then clicks the Add button. Here goes: 1. Call a stored procedure called AddCompany to insert the company name from the Company Name textbox into the COMPANY table and return the @@IDENTITY of the company name just input into the database back to a label on the form. THIS IS WORKING.
9
4136
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table. When calling the stored procedure from VB.NET, in the CommandText, can I just say "INSERT_INTO_MYTABLE '12345'" instead of calling it with "INSERT_INTO_MYTABLE" then do the following : OleDbCommand2.Parameters.Add("@Account", SqlDbType.VarChar, 10)...
2
6381
by: Krij | last post by:
Hi, I'm a student and I have the following working example that troubles me (in SQL Server 2005): CREATE PROCEDURE dbo.CustomersOrderHistory ( @Firstname varchar(7) OUTPUT) AS SELECT dbo.Customers.Firstname, dbo.Orders.Orderdate, dbo.Orderdetails.Orderamount, dbo.Orderdetails.Orderprice,
2
4100
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
12
5131
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from the stored procedure which is an INT. I want to access this return value on my ASP/VBScript page, but do not know how to access it. Here is my code so far:
0
8290
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
8815
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
8708
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...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2716
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
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
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.