Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Output variable does not return result when using SqlCommand?


Output variable does not return result when using SqlCommand?

Author
Message
les-981979
les-981979
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 16
I am using VS2005 connecting to a hosted SQL 2005 server.

I have a stored procedure that looks like this...
--------------------------------------------------------
ALTER PROCEDURE [dbo].[GetSupplierID]
@SupplierName varchar(100),
@SupplierBranch varchar(100) = NULL,
@SupplierID bigint OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT @SupplierID = SupplierID
FROM SU_Suppliers
WHERE (SupplierName = @SupplierName) AND
(@SupplierBranch IS NULL OR SupplierBranch = @SupplierBranch)
END
---------------------------------------------------------
if I run the stored procedure through SQL management studio it works fine and the SupplierID is returned in the OUTPUT parameter '@SupplierID'

When I try to do this in code it doesn't return anything and the result of the SQLCommand is -1

C# code...
----------------------------------------------------------

SqlCommand getCoID = new SqlCommand();
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SupportConnectionString"].ConnectionString);
getCoID.Connection = Conn;
getCoID.CommandType = CommandType.StoredProcedure;
getCoID.CommandText = "GetSupplierID";
getCoID.Parameters.Add("@SupplierName", SqlDbType.VarChar, 100).Value = ThisUser.UserName;
getCoID.Parameters.Add("@SupplierBranch", SqlDbType.VarChar, 100);
getCoID.Parameters.Add("@SupplierID", SqlDbType.BigInt);
getCoID.Parameters["@SupplierID"].Direction = ParameterDirection.Output;
Conn.Open();
int UCo = getCoID.ExecuteNonQuery();

Session["UserCompany"] = getCoID.Parameters["@SupplierID"].Value;
----------------------------------------------------------

Can anyone tell me why this SqlCommand is failing??

Thanks
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 14858
Are you sure that This.UserName is returning the name in the format you need?



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
ALZDBA
ALZDBA
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6974 Visits: 8839
What's your db collation ??

(case sensitive , accent sensitive, ... ??)

Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution" :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
les-981979
les-981979
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 16
Jack,

Thanks a bunch man! Sometimes it helps to just get a different look at things. The ThisUser.Username is from the ASP.NET user membership after login. The problem was that I didn't want to use the username I was supposed to use the comments field where I store the company name of the user.

Working fine now.

Les
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search