Solved

ADO.NET passing SQL InputOutput parameter problem

Posted on 2004-09-09
12
683 Views
Last Modified: 2008-02-01
Hi.

I have problem with ParameterDirection.InputOutput.

I'm still in problem when passing imput value to InputOutput parameter. When i look on SQL I see that there is alway NULL pased regardles to what I set to Value.

Code:
SqlC_GetActualVersion.Parameters("@version").Value = CDec(Version)
SqlC_GetActualVersion.Parameters("@version").Direction = ParameterDirection.InputOutput
SqlC_GetActualVersion.Connection.Open()
SqlC_GetActualVersion.ExecuteReader()
SqlC_GetActualVersion.Connection.Close()
Version = CType(SqlC_GetActualVersion.Parameters("@version").Value, String)

SQL command send in background:
declare @P1 numeric(8,4)
SET @P1=NULL
EXEC dbo.GetActualVersion @version=@P1 output


Return value is Ok.
0
Comment
Question by:patrikt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 10

Expert Comment

by:EBatista
ID: 12014845
you cannot access the reader value if you close the connection, and if you actually dont need the db cursor then use ExecuteNonQuery method instead:

SqlC_GetActualVersion.Connection.Open()
SqlC_GetActualVersion.ExecuteNonQuery()
SqlC_GetActualVersion.Connection.Close()
Version = CType(SqlC_GetActualVersion.Parameters("@version").Value, String)

regards
0
 
LVL 10

Expert Comment

by:EBatista
ID: 12015156
also ExecuteReader() method is for retrieve "records" from the database, it wont perform any insert or update, so if you are trying to write back changes or inserting rows to the db you have to use ExecuteNonQuery.
0
 
LVL 12

Author Comment

by:patrikt
ID: 12015157
Yes I know that. ExecuteReader is only "fosil" after my experimenting.

I'have found "way arround". Everything is working if ALL parameters are defined as InpurOutput and output in Sp. But why to do this? It looks like bug in SqlCommand.

Patrik
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 10

Expert Comment

by:EBatista
ID: 12015255
nope, no bugs, can u paste the whole code for the stored procedure
0
 
LVL 20

Expert Comment

by:ihenry
ID: 12017414
>> you cannot access the reader value if you close the connection
In addition to EBatista comment,
you can't as well retrieve the output parameters until close the datareader

Try this,
SqlC_GetActualVersion.Connection.Open()
SqlDataReader dr = SqlC_GetActualVersion.ExecuteReader()
dr.Close()
Version = CType(SqlC_GetActualVersion.Parameters("@version").Value, String)
SqlC_GetActualVersion.Connection.Close()
0
 
LVL 12

Author Comment

by:patrikt
ID: 12031223
There is no problem with reading parameters. There is problem with passing paramaters.
I investigatet this and there is my result:
1) Problem is only with SQLCommand. OLEDBCommand works well.
2) Problem is only when stored procedure has some but not all paramaters as output.
3) If 1 and 2 is true and I assign value to output parameter it is set to null when calling stored procedure regardles I set it as InputOutput.

Workaround is to set all parametere in stored procedure as output. Then it works OK. But it is still bug.

Any comments?
0
 
LVL 5

Accepted Solution

by:
ajitanand earned 500 total points
ID: 12109537
hello,

The correct order for setting parameters is:
Create a SQLParameter Object
Assign the parameter direction and Set the value

Then add to the parameters collection of the Command, and then execute.

rgds,
Ajit Anand
0
 
LVL 12

Author Comment

by:patrikt
ID: 12367231
This question is not answered. I went arround the problem and have no time to investigate the source of errors.

Please give some points to Experts participating, but don't PAQ this question.

Patrik
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question