Solved

How do I execute and return one output parameter from a stored procedure which returns two output parameters  in ASP.NET?

Posted on 2011-02-11
8
437 Views
Last Modified: 2012-08-13
I have a stored procedure which returns two output parameters.How do I execute and return one output parameter from a stored procedure which returns two output parameters  in ASP.NET?
0
Comment
Question by:AppDevs
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:jmro20
ID: 34873805
When you create the parameters there is a property called Direction and set it to Output

After executing evaluate that parameter value.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 34873937
can you show the code both in asp.net and store procedure for simplification?
0
 
LVL 3

Accepted Solution

by:
jmro20 earned 250 total points
ID: 34873945
You can do something like this:

'Sql Command
mSqlCommand = New SqlCommand
mSqlCommand.Connection = YourConnection
mSqlCommand.CommandTimeout = iTimeout
mSqlCommand.CommandText = yourSP
mSqlCommand.CommandType = CommandType.StoredProcedure

Dim param As SqlParameter() = { _
            New SqlParameter("@ParameterInput", SqlDbType.VarChar, 2), _
            New SqlParameter("@ParameterOutput1", SqlDbType.VarChar, 4), _
            New SqlParameter("@ParameterOutput2", SqlDbType.VarChar)
        }

param(0).Value = yourInputValue
param(1).Direction = ParameterDirection.Output
param(2).Direction = ParameterDirection.Output

'Add Parameters
For Each param In paramArr
   mSqlCommand.Parameters.Add(param)
 Next

'Exec NonQuery
 mSqlCommand.ExecuteNonQuery()
OutputValue1 = Convert.ToString(mSqlCommand.Parameters"("@ParameterOutput1).Value)
OutputValue2 = Convert.ToString(mSqlCommand.Parameters"("@ParameterOutput2).Value)

Open in new window

0
 
LVL 3

Expert Comment

by:jmro20
ID: 34873972
There was an error in the loop to add parameters
'Sql Command
mSqlCommand = New SqlCommand
mSqlCommand.Connection = YourConnection
mSqlCommand.CommandTimeout = iTimeout
mSqlCommand.CommandText = yourSP
mSqlCommand.CommandType = CommandType.StoredProcedure

Dim paramArr As SqlParameter() = { _
            New SqlParameter("@ParameterInput", SqlDbType.VarChar, 2), _
            New SqlParameter("@ParameterOutput1", SqlDbType.VarChar, 4), _
            New SqlParameter("@ParameterOutput2", SqlDbType.VarChar)
        }

param(0).Value = yourInputValue
param(1).Direction = ParameterDirection.Output
param(2).Direction = ParameterDirection.Output

'Add Parameters
For Each param As SqlParameter In paramArr
   mSqlCommand.Parameters.Add(param)
 Next

'Exec NonQuery
 mSqlCommand.ExecuteNonQuery()
OutputValue1 = Convert.ToString(mSqlCommand.Parameters"("@ParameterOutput1).Value)
OutputValue2 = Convert.ToString(mSqlCommand.Parameters"("@ParameterOutput2).Value)

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Assisted Solution

by:sarabhai
sarabhai earned 250 total points
ID: 34874154
following is just sample for you...
what error come at your code.

SqlCommand cmd = new SqlCommand("CustOrderOne", cn);
cmd.CommandType=CommandType.StoredProcedure ;
SqlParameter parm=new SqlParameter("@CustomerID",SqlDbType.NChar) ;
parm.Value="ALFKI";
parm.Direction =ParameterDirection.Input ;
cmd.Parameters.Add(parm);
SqlParameter parm2=new SqlParameter("@ProductName",SqlDbType.VarChar);
parm2.Size=50;
parm2.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm2);
SqlParameter parm3=new SqlParameter("@Quantity",SqlDbType.Int);
parm3.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm3);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34874473
That's almost the same I said.
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34874489
Correcting another error:
OutputValue1 = Convert.ToString(mSqlCommand.Parameters("@ParameterOutput1").Value)
OutputValue2 = Convert.ToString(mSqlCommand.Parameters("@ParameterOutput2").Value)
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34874636
>How do I execute and return one output parameter from a stored procedure which returns two output parameters  in ASP.NET?

Provide it two parameters but ignore the return value of one parameter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
More on Time zones in vb 2010 12 37
Not showing page correctly 3 29
Entity Framework 7 28
Help with C#, MVC, razor. 6 21
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now