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);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add("@iApplicationID",
SqlDbType.Int);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@vcApplicationConstName",
SqlDbType.VarChar);
param.Value = sAppConstName;
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
// check for null here
if (cmd.Parameters["@iApplicationID"].Value != null){
iID = (int)cmd.Parameters["@iApplicationID"].Value;
}
else{
throw new ApplicationException("Unable to retrieve Application ID for
application: " + sAppConstName);
}
Stored Procedure (for test purposes):
CREATE PROCEDURE [dbo].[sp_BSQ_GetApplicationID]
@vcApplicationConstName varchar(50),
@iApplicationID int OUTPUT
AS
SET @iApplicationID = null
GO
Thanks in advance for your help.
Steven 4 2168
Hello
Compare with DBNull.Value instead of null.
if (cmd.Parameters["@iApplicationID"].Value != DBNull.Value)
{
.....
}
Best regards,
Sherif
"Steven" <st************@virgin.net> wrote in message
news:bo*******************@news.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_GetApplicationID", conn); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add("@iApplicationID", SqlDbType.Int); param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@vcApplicationConstName", SqlDbType.VarChar); param.Value = sAppConstName;
if (conn.State == ConnectionState.Closed) conn.Open();
cmd.ExecuteNonQuery(); conn.Close();
// check for null here if (cmd.Parameters["@iApplicationID"].Value != null){ iID = (int)cmd.Parameters["@iApplicationID"].Value; } else{ throw new ApplicationException("Unable to retrieve Application ID for application: " + sAppConstName); }
Stored Procedure (for test purposes):
CREATE PROCEDURE [dbo].[sp_BSQ_GetApplicationID]
@vcApplicationConstName varchar(50), @iApplicationID int OUTPUT
AS
SET @iApplicationID = null
GO
Thanks in advance for your help.
Steven
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_GetApplicationID", conn); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add("@iApplicationID", SqlDbType.Int); param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@vcApplicationConstName", SqlDbType.VarChar); param.Value = sAppConstName;
if (conn.State == ConnectionState.Closed) conn.Open();
cmd.ExecuteNonQuery(); conn.Close();
// check for null here if (cmd.Parameters["@iApplicationID"].Value != null){ iID = (int)cmd.Parameters["@iApplicationID"].Value; } else{ throw new ApplicationException("Unable to retrieve Application ID for application: " + sAppConstName); }
Stored Procedure (for test purposes):
CREATE PROCEDURE [dbo].[sp_BSQ_GetApplicationID]
@vcApplicationConstName 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["@iApplicationID"].Value != DBNull.Value
Dmitry
"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****@tinkertoys.net> wrote in message
news:s5********************@adelphia.com... "Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:u9**************@TK2MSFTNGP09.phx.gbl... Hello
Compare with DBNull.Value instead of null.
if (cmd.Parameters["@iApplicationID"].Value != DBNull.Value) { .... }
Best regards, Sherif
"Steven" <st************@virgin.net> wrote in message news:bo*******************@news.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_GetApplicationID", conn); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add("@iApplicationID", SqlDbType.Int); param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@vcApplicationConstName", SqlDbType.VarChar); param.Value = sAppConstName;
if (conn.State == ConnectionState.Closed) conn.Open();
cmd.ExecuteNonQuery(); conn.Close();
// check for null here if (cmd.Parameters["@iApplicationID"].Value != null){ iID = (int)cmd.Parameters["@iApplicationID"].Value; } else{ throw new ApplicationException("Unable to retrieve Application ID
for application: " + sAppConstName); }
Stored Procedure (for test purposes):
CREATE PROCEDURE [dbo].[sp_BSQ_GetApplicationID]
@vcApplicationConstName 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Dino L. |
last post by:
How can I run stored procedure (MSSQL) ?
|
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...
|
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....
|
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 ...
|
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
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |