C# execute sql stored procedure with output parameters

C# execute sql stored procedure with output parameters

Posted: MaxT Date of post: 17.07.2017

I am having a problem returning an output parameter from a Sql Server stored procedure into a C variable.

c# execute sql stored procedure with output parameters

I have read the other posts concerning this, not only here but on other sites, and I cannot get it to work. Here is what I currently have. Currently I am just trying to print the value that comes back. The following code returns a null value.

c# execute sql stored procedure with output parameters

What I an trying to return is the primary key. Does this work in your environment, too? I can't say why your original code won't work - but when I do this here, VS and SQL Server R2, it just works flawlessly If you don't get back a value - then I suspect your table Contracts might not really have a column with the IDENTITY property on it.

Before changing stored procedure please check what is the output of your current one. In SQL Server Management run following:. Object cannot be cast from DBNull to other types.

I'll take this up again tomorrow. I'm off to my other job,".

C - Citigroup Stock quote - ywepubuy.web.fc2.com

In your C code, you are using transaction for the command. Just commit the transaction and after that access your parameter value, you will get the value. By posting your answer, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about How to make money from ezine Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community.

c# execute sql stored procedure with output parameters

Stack Overflow is a community of 7. Reviews from belarus to work with the binary options them; it only takes a minute: Using stored procedure output parameters in C Ask Question.

Print "Insert Exception Type: Servy k 17 Gary 2 12 Do I use the set command to do this? I replaced c# execute sql stored procedure with output parameters select statement with the following Of course you can confirm that the new record is actually inserted, right?

Could you show more of your C code the opening of the connection, the creation of command etc. Yes I can see the row in the database.

Could you try to commit the transaction before reading the output parameter? Add " ContractNumber", SqlDbType. VarChar, 7 ; cmd. Add " NewId", SqlDbType. I appreciate your time in answering this for those of us Googling it years later.

Output; needs to be altered c# execute sql stored procedure with output parameters cmd. AddWithValue " NewId", SqlDbType. You should check out Can we stop using AddWithValue already? AddWithValue - it can lead to unexpected and surprising results In SQL Server Management run following: This may give you some hints of why your out param is not filled.

TarasB 1, 15 This just about works as an answer, but probably should be a comment, however I can't convert it because of the multiple character usage even mods are denied that right and I can't be bothered editing it to make it work.

I'm off to my other job," I believe the Id column in your SQL Table isn't a identity column.

How to run the stored procedure that has OUTPUT parameter from C#? - Stack Overflow

But this answers not the question why the output parameter for the new id returns null. With ExecuteScalar you can retrieve a return value not an output parameter. I also receive the following error when I make this change Object reference not set to an instance of an object.

ContractNumber is the value being added to the table. The primary key is called Id. ExecuteScalar returns an object, not an int.

I receive an exception when I execute the code. Sign up or log in StackExchange. Sign up using Facebook.

sql server - SQL output parameters in C# - Stack Overflow

Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with JavaScript enabled. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

Rating 4,1 stars - 687 reviews
inserted by FC2 system