473,396 Members | 1,892 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,396 software developers and data experts.

Need to return a string ( varchar(40) ) from MS-SQLSERVER storedprocedure

I've been banging my head on this for hours and it's something that
should be very very easy. I have a Stored Procedure in a MS-SQL 2000
server that returns a Varchar(40) and I want to grab that value in
C-Sharp the easiest way possible. I've tried multiple ways, using
ExecuteNonQuery, ExecuteScalar, etc. and I just can't get the value.

I've tested the SP in SQL Manager and it returns the value just fine.

This is the SP:

--------------------------------------------------------
CREATE PROCEDURE [dbo].[GetJurisName] (
@in_juris_id INT, -- ID of the Jurisdiction
@in_juris_type INT, -- Type of the Jurisdiction
@in_juris_country INT, -- ID of the Country
@out_juris_name VARCHAR(40) OUTPUT -- Returned name of the Jurisdiction
)
AS
BEGIN
SET @out_juris_name = 'UNKNOWN'

SELECT @out_juris_name = juris_name
FROM dl_jurisdiction
WHERE juris_id = @in_juris_id AND juris_type = @in_juris_type AND
juris_country = @in_juris_country
END
--------------------------------------------------------

This is the latest way I've been trying to get the value, and it's
keeping the empty string in the 4th parameter:

--------------------------------------------------------
_Connection.Open();

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "GetJurisName";
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Connection = _Connection;

//Handle the parameters
SqlParameter[] arParameters = new SqlParameter[4];
arParameters[ 0 ] = sqlCmd.Parameters.Add("@in_juris_id", SqlDbType.Int);
arParameters[ 0 ].Value = nJurisID;
arParameters[ 0 ].Direction = ParameterDirection.Input;
arParameters[ 1 ] = sqlCmd.Parameters.Add("@in_juris_type", SqlDbType.Int);
arParameters[ 1 ].Value = nJurisType;
arParameters[ 1 ].Direction = ParameterDirection.Input;
arParameters[ 2 ] = sqlCmd.Parameters.Add("@in_juris_country",
SqlDbType.Int);
arParameters[ 2 ].Value = nJurisCountry;
arParameters[ 2 ].Direction = ParameterDirection.Input;
arParameters[ 3 ] = sqlCmd.Parameters.Add("@out_juris_name",
SqlDbType.VarChar);
arParameters[ 3 ].Value = string.Empty;
arParameters[ 3 ].Direction = ParameterDirection.Output;

sqlCmd.ExecuteNonQuery();
string strJurisName = arParameters[ 3 ].Value.ToString();
--------------------------------------------------------

I'm not married to the above code, I just want the thing to work. Any
help is appreciated.

TIA,
-BEP
Jul 6 '06 #1
4 3963
Brian,

It doesn't appear that you are setting the size of the output parameter.
When adding the last parameter, your code should read:

arParameters[ 3 ] = sqlCmd.Parameters.Add("@out_juris_name",
SqlDbType.VarChar, 40);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Parker" <be******@yahoo.comwrote in message
news:RRgrg.3896$nK.1171@dukeread05...
I've been banging my head on this for hours and it's something that should
be very very easy. I have a Stored Procedure in a MS-SQL 2000 server that
returns a Varchar(40) and I want to grab that value in C-Sharp the easiest
way possible. I've tried multiple ways, using ExecuteNonQuery,
ExecuteScalar, etc. and I just can't get the value.

I've tested the SP in SQL Manager and it returns the value just fine.

This is the SP:

--------------------------------------------------------
CREATE PROCEDURE [dbo].[GetJurisName] (
@in_juris_id INT, -- ID of the Jurisdiction
@in_juris_type INT, -- Type of the Jurisdiction
@in_juris_country INT, -- ID of the Country
@out_juris_name VARCHAR(40) OUTPUT -- Returned name of the Jurisdiction
)
AS
BEGIN
SET @out_juris_name = 'UNKNOWN'

SELECT @out_juris_name = juris_name
FROM dl_jurisdiction
WHERE juris_id = @in_juris_id AND juris_type = @in_juris_type AND
juris_country = @in_juris_country
END
--------------------------------------------------------

This is the latest way I've been trying to get the value, and it's keeping
the empty string in the 4th parameter:

--------------------------------------------------------
_Connection.Open();

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "GetJurisName";
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Connection = _Connection;

//Handle the parameters
SqlParameter[] arParameters = new SqlParameter[4];
arParameters[ 0 ] = sqlCmd.Parameters.Add("@in_juris_id", SqlDbType.Int);
arParameters[ 0 ].Value = nJurisID;
arParameters[ 0 ].Direction = ParameterDirection.Input;
arParameters[ 1 ] = sqlCmd.Parameters.Add("@in_juris_type",
SqlDbType.Int);
arParameters[ 1 ].Value = nJurisType;
arParameters[ 1 ].Direction = ParameterDirection.Input;
arParameters[ 2 ] = sqlCmd.Parameters.Add("@in_juris_country",
SqlDbType.Int);
arParameters[ 2 ].Value = nJurisCountry;
arParameters[ 2 ].Direction = ParameterDirection.Input;
arParameters[ 3 ] = sqlCmd.Parameters.Add("@out_juris_name",
SqlDbType.VarChar);
arParameters[ 3 ].Value = string.Empty;
arParameters[ 3 ].Direction = ParameterDirection.Output;

sqlCmd.ExecuteNonQuery();
string strJurisName = arParameters[ 3 ].Value.ToString();
--------------------------------------------------------

I'm not married to the above code, I just want the thing to work. Any
help is appreciated.

TIA,
-BEP

Jul 7 '06 #2
Brian,
I think the reason why this may be confusing is if I remember correctly, you
must close the connection before the values of output parameters are
available. Personally, I rarely do this, if I only have one value I simply
select it out and use executeScalar which returns an object (which in your
case you would simply cast to a string.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Brian Parker" wrote:
I've been banging my head on this for hours and it's something that
should be very very easy. I have a Stored Procedure in a MS-SQL 2000
server that returns a Varchar(40) and I want to grab that value in
C-Sharp the easiest way possible. I've tried multiple ways, using
ExecuteNonQuery, ExecuteScalar, etc. and I just can't get the value.

I've tested the SP in SQL Manager and it returns the value just fine.

This is the SP:

--------------------------------------------------------
CREATE PROCEDURE [dbo].[GetJurisName] (
@in_juris_id INT, -- ID of the Jurisdiction
@in_juris_type INT, -- Type of the Jurisdiction
@in_juris_country INT, -- ID of the Country
@out_juris_name VARCHAR(40) OUTPUT -- Returned name of the Jurisdiction
)
AS
BEGIN
SET @out_juris_name = 'UNKNOWN'

SELECT @out_juris_name = juris_name
FROM dl_jurisdiction
WHERE juris_id = @in_juris_id AND juris_type = @in_juris_type AND
juris_country = @in_juris_country
END
--------------------------------------------------------

This is the latest way I've been trying to get the value, and it's
keeping the empty string in the 4th parameter:

--------------------------------------------------------
_Connection.Open();

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "GetJurisName";
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Connection = _Connection;

//Handle the parameters
SqlParameter[] arParameters = new SqlParameter[4];
arParameters[ 0 ] = sqlCmd.Parameters.Add("@in_juris_id", SqlDbType.Int);
arParameters[ 0 ].Value = nJurisID;
arParameters[ 0 ].Direction = ParameterDirection.Input;
arParameters[ 1 ] = sqlCmd.Parameters.Add("@in_juris_type", SqlDbType.Int);
arParameters[ 1 ].Value = nJurisType;
arParameters[ 1 ].Direction = ParameterDirection.Input;
arParameters[ 2 ] = sqlCmd.Parameters.Add("@in_juris_country",
SqlDbType.Int);
arParameters[ 2 ].Value = nJurisCountry;
arParameters[ 2 ].Direction = ParameterDirection.Input;
arParameters[ 3 ] = sqlCmd.Parameters.Add("@out_juris_name",
SqlDbType.VarChar);
arParameters[ 3 ].Value = string.Empty;
arParameters[ 3 ].Direction = ParameterDirection.Output;

sqlCmd.ExecuteNonQuery();
string strJurisName = arParameters[ 3 ].Value.ToString();
--------------------------------------------------------

I'm not married to the above code, I just want the thing to work. Any
help is appreciated.

TIA,
-BEP
Jul 7 '06 #3
Nicholas Paldino [.NET/C# MVP] wrote:
Brian,

It doesn't appear that you are setting the size of the output parameter.
When adding the last parameter, your code should read:

arParameters[ 3 ] = sqlCmd.Parameters.Add("@out_juris_name",
SqlDbType.VarChar, 40);
Thanks, Nick. That worked.

-BEP
Jul 7 '06 #4
Peter Bromberg [C# MVP] wrote:
Brian,
I think the reason why this may be confusing is if I remember correctly, you
must close the connection before the values of output parameters are
available. Personally, I rarely do this, if I only have one value I simply
select it out and use executeScalar which returns an object (which in your
case you would simply cast to a string.
Nick set me straight with the size of the Varchar being sent in and that
fixed it. But, I prefer to use ExecuteScalar() if I can, so I'm going
to try to convert it back to that and send in the size.

Thanks!
-BEP
Jul 7 '06 #5

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

Similar topics

5
by: Indy Tech | last post by:
Need to maintain or re-write some DOS apps. I was told that Microsoft Visual C++ 1.52, being discontinued, was something I could legally get a copy of from someone. Is this true? And if so,...
2
by: Jeffrey D Moncrieff | last post by:
I am trying to create a data base for colleting date for my research project. I have run in to a couple of problems and I need it fast ASAP I need it to be able to add data and edit them and view...
0
by: Martin Platt | last post by:
Hi, I'm looking into various options for a fairly simple backup utility for our application. I have no problems being able to backup, find restorable backup files, and restore then. What I'd...
7
by: Ioannis Vranos | last post by:
I had reported this as a bug: Description: Default indexed property of System::String crashes for object with stack semantics. int main()
4
by: AssanKhan Ismail | last post by:
Im using an C#'s user defined private function on which i want to return more than one value (like int and string ) and from that function. please let me know in advance.. assankhan Ismail
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
1
by: Steve Harp | last post by:
Hi All, I'm not sure this can be done in a calculated field but here's what I need. I have a table that stores time off for employees (such as vacation, sick time, etc). An employee can take...
2
by: ldphill | last post by:
I have the following database model: Employee -Table PK EmpSSN varchar (13) FirstNm varchar (40) ...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
10
by: Atara | last post by:
Suppose I have the following functions: Public Function myF1ToStream(...) As System.IO.MemoryStream . . . Dim ms As New System.IO.MemoryStream(buf) Return ms End Function Public Function...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.