473,394 Members | 2,071 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,394 software developers and data experts.

Stored procedure with output parameter

Hi,
I have a generic function that executes a stored procedure & returns a
data table.
Code:
===
public static DataTable ExecuteStoredProcedure(string
strProc,SqlParameter[] paramArray)
{
SqlCommand command = new SqlCommand(strProc, connection);
command.CommandType = CommandType.StoredProcedure;
for(int i=0;i<paramArray.Length;i++)
command.Parameters.Add(paramArray[i]);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
connection.Close();
return(table);
}

The stored proc is used for paging, it takes page number & page size as
input parameters & returns the records for a particular page.

This code works fine and I use the data table returned by this function
to bind a datagrid and also to do some other stuff during the
itemdatabound of the grid.

Now I want the stored proc to return the total number of records (it
returns only n records of a particular page) in the database. I can not
use data table's rowcount coz it will always give the page size 10),
not the total number of records in the database.

So I added an OUTPUT parameter to the stored proc which returns the
total number of records as well.

Now my problem is how do I change this function to take care of this
output parameter.
I can add the output parameter to the parameter array and execute the
stored proc but the data table will not contain the output paramerter's
value. I can get it using command.parameter("@out") but then I can not
return both the data table & this value from the function.

Is there any way I can tweak this function to return both the values? I
want the calling function to receive both the output paramer's value
and also the records returned by the stored proc.

Thanks,
Ashish.

Dec 6 '06 #1
7 8537
You need to change the parameter's direction to InputOutput; the default is
Input. Then it *will* be updated as long as the SP also declares it as OUT.
And for ref, I'm 99% that you can get both table data a result value - you
just need to add a result parameter - I believe that means a parameter with
direction ReturnValue.

Marc
Dec 6 '06 #2
Couple of options, either add an out parameter to the function

private void footest {
int i;
foo(out i);
Console.WriteLine(i.ToString());
}
private bool foo(out int pFoo)
{
pFoo = 1;
return true;
}

Alternatively create an object or structure to hold both the DataTable
and the count and return that.
public class Holder
{
private DataTable _table;
private int _count;
....
}

as****@gmail.com wrote:
Hi,
I have a generic function that executes a stored procedure & returns a
data table.
Code:
===
public static DataTable ExecuteStoredProcedure(string
strProc,SqlParameter[] paramArray)
{
SqlCommand command = new SqlCommand(strProc, connection);
command.CommandType = CommandType.StoredProcedure;
for(int i=0;i<paramArray.Length;i++)
command.Parameters.Add(paramArray[i]);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
connection.Close();
return(table);
}

The stored proc is used for paging, it takes page number & page size as
input parameters & returns the records for a particular page.

This code works fine and I use the data table returned by this function
to bind a datagrid and also to do some other stuff during the
itemdatabound of the grid.

Now I want the stored proc to return the total number of records (it
returns only n records of a particular page) in the database. I can not
use data table's rowcount coz it will always give the page size 10),
not the total number of records in the database.

So I added an OUTPUT parameter to the stored proc which returns the
total number of records as well.

Now my problem is how do I change this function to take care of this
output parameter.
I can add the output parameter to the parameter array and execute the
stored proc but the data table will not contain the output paramerter's
value. I can get it using command.parameter("@out") but then I can not
return both the data table & this value from the function.

Is there any way I can tweak this function to return both the values? I
want the calling function to receive both the output paramer's value
and also the records returned by the stored proc.

Thanks,
Ashish.
Dec 6 '06 #3
Sorry - I misread the question; in my example, the caller *will* see the
update, but in their array of the parameters. You can also use separate
parameters, but then you need to watch for conflicts - i.e. what happens if
the caller has already added a ReturnValue parameter - then you can't add
your own, etc.

Marc
Dec 6 '06 #4
Thank you for your reply Marc.

I am calling this function like this:
========================================

SqlParameter[] paramArray=new SqlParameter[3];

SqlParameter param1=new SqlParameter();
param1.Direction=ParameterDirection.Input;
param1.ParameterName="@CurrentPage";
param1.Value=iCurPageNumber;
paramArray[0]=(param1);

SqlParameter param2=new SqlParameter();
param2.Direction=ParameterDirection.Input;
param2.ParameterName="@PageSize";
param2.Value=iPageSize;
paramArray[1]=(param2);

SqlParameter param3=new SqlParameter();
param3.Direction=ParameterDirection.Output;
param3.ParameterName="@RowCount";
param3.Size=10;
paramArray[2]=(param3);

dataTable=DAC.ExecuteStoredProcedure1("pr_GetRecor ds",paramArray);

========================================

Are you saying that I have to use
param3.Direction=ParameterDirection.ReturnValue; ?

If I do this, I get an error saying that the proc expects a parameter
"@out" which was not supplied.

-Ashish.
Marc Gravell wrote:
You need to change the parameter's direction to InputOutput; the default is
Input. Then it *will* be updated as long as the SP also declares it as OUT.
And for ref, I'm 99% that you can get both table data a result value - you
just need to add a result parameter - I believe that means a parameter with
direction ReturnValue.

Marc
Dec 6 '06 #5
My stored procedure looks like this:

CREATE PROCEDURE [dbo].[pr_ProcName]
@CurrentPage INT=NULL,
@PageSize INT=NULL,
@RowCount INT OUTPUT
AS
------------------
some code...
......
.......
.......
-----------------

SELECT * FROM #tmp_Tbl

SELECT @RowCount=COUNT(PKTMP) FROM #tmp_Tbl

Drop table #tmp_Tbl

-Ashish.

Marc Gravell wrote:
You need to change the parameter's direction to InputOutput; the default is
Input. Then it *will* be updated as long as the SP also declares it as OUT.
And for ref, I'm 99% that you can get both table data a result value - you
just need to add a result parameter - I believe that means a parameter with
direction ReturnValue.

Marc
Dec 6 '06 #6
It is working fine with the existing code.
Yor are right Marc, the parameter (param3) is updated with the output
parameter's value from the stored proc.
iTotRecords=Convert.ToInt32(param3.Value.ToString( ));
I was thinking that this value will not be available in the calling
function as I am passing this parameter to another function.
Thank you again,
Ashish

as****@gmail.com wrote:
Thank you for your reply Marc.

I am calling this function like this:
========================================

SqlParameter[] paramArray=new SqlParameter[3];

SqlParameter param1=new SqlParameter();
param1.Direction=ParameterDirection.Input;
param1.ParameterName="@CurrentPage";
param1.Value=iCurPageNumber;
paramArray[0]=(param1);

SqlParameter param2=new SqlParameter();
param2.Direction=ParameterDirection.Input;
param2.ParameterName="@PageSize";
param2.Value=iPageSize;
paramArray[1]=(param2);

SqlParameter param3=new SqlParameter();
param3.Direction=ParameterDirection.Output;
param3.ParameterName="@RowCount";
param3.Size=10;
paramArray[2]=(param3);

dataTable=DAC.ExecuteStoredProcedure1("pr_GetRecor ds",paramArray);

========================================

Are you saying that I have to use
param3.Direction=ParameterDirection.ReturnValue; ?

If I do this, I get an error saying that the proc expects a parameter
"@out" which was not supplied.

-Ashish.
Marc Gravell wrote:
You need to change the parameter's direction to InputOutput; the default is
Input. Then it *will* be updated as long as the SP also declares it as OUT.
And for ref, I'm 99% that you can get both table data a result value - you
just need to add a result parameter - I believe that means a parameter with
direction ReturnValue.

Marc
Dec 6 '06 #7
Thank you for your suggestion. I was not aware of the out parameter to
a function.
My code is working as it is, I was just confused!
Thanks,
Ashish

DeveloperX wrote:
Couple of options, either add an out parameter to the function

private void footest {
int i;
foo(out i);
Console.WriteLine(i.ToString());
}
private bool foo(out int pFoo)
{
pFoo = 1;
return true;
}

Alternatively create an object or structure to hold both the DataTable
and the count and return that.
public class Holder
{
private DataTable _table;
private int _count;
...
}

as****@gmail.com wrote:
Hi,
I have a generic function that executes a stored procedure & returns a
data table.
Code:
===
public static DataTable ExecuteStoredProcedure(string
strProc,SqlParameter[] paramArray)
{
SqlCommand command = new SqlCommand(strProc, connection);
command.CommandType = CommandType.StoredProcedure;
for(int i=0;i<paramArray.Length;i++)
command.Parameters.Add(paramArray[i]);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
connection.Close();
return(table);
}

The stored proc is used for paging, it takes page number & page size as
input parameters & returns the records for a particular page.

This code works fine and I use the data table returned by this function
to bind a datagrid and also to do some other stuff during the
itemdatabound of the grid.

Now I want the stored proc to return the total number of records (it
returns only n records of a particular page) in the database. I can not
use data table's rowcount coz it will always give the page size 10),
not the total number of records in the database.

So I added an OUTPUT parameter to the stored proc which returns the
total number of records as well.

Now my problem is how do I change this function to take care of this
output parameter.
I can add the output parameter to the parameter array and execute the
stored proc but the data table will not contain the output paramerter's
value. I can get it using command.parameter("@out") but then I can not
return both the data table & this value from the function.

Is there any way I can tweak this function to return both the values? I
want the calling function to receive both the output paramer's value
and also the records returned by the stored proc.

Thanks,
Ashish.
Dec 6 '06 #8

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

Similar topics

1
by: Bari Allen | last post by:
I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First varchar(80) OUTPUT ,...
4
by: laurenq uantrell | last post by:
I need to get the value of an output parameter back into my VBA function calling a stored procedure. I'm using the following construction to append a new record in a SQL Server table: ...
7
by: VMI | last post by:
Can I call a stored proc from .net with a parm that changes during the SP? For example, I call the SP with parameter sMyVariable (which changes in the SP), and when I access it after the SP is...
1
by: Craig Buchanan | last post by:
Has any one been able to get MS DataAccess Block code to populate a return parameter in the ExecuteReader method? If so, how? Thanks, Craig Buchanan
2
by: Bari Allen | last post by:
ASP Classic question: I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First...
0
by: rockdale | last post by:
Hi, All How to get the output parameter's value when you use the SQLHelper (Microsoft Data Access Block)? When I try to access my ourput parm I got the following error. ...
5
by: Alan T | last post by:
I have a string passed from another function, eg list_employee 4 This will call the stored procedure list_employee to get details of employee of id 4. Is there a way to just use this...
9
by: VK | last post by:
Hello, Can some one suggest me a way of putting Stored procedure output into a file, the file should be available on my Computer, I don't want to use FTP process. Thanks vk
3
by: rhustang | last post by:
Hi guys, Very, very new to databases in general. Have been spending a couple days trying to figure out the following (names are made up to protect the innocent): I want to pull CarModel out of...
1
by: David Gillen | last post by:
Hello. I'm having difficulty getting the value of output parameters from stored procedures on a SQL server. I understand that this just doesn't work with odbc so I've tried both the mssql...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.