473,386 Members | 1,652 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,386 software developers and data experts.

Getting an Output Parameter from a SqlDataSource


This is a my solution to getting an Output parameter from a SqlDataSource.

I have seen a few scant articles but none of them take it all the way to
a solution. Hopefully this will help some poor soul.

Situation: I want to do a lookup using a stored procedure for each
value in a Row within a GridView.

I use a lookup function in my code behind, evaluating the necessary
bound fields. The problem is the SqlDataSource representing the stored
procedure was returning an empty string after running the .Select() method.

<asp:Label
ID="FieldValue"
runat="server" Text='<%#
lookupValueName((string)DataBinder.Eval(Container, "DataItem.FieldName"),(string)DataBinder.Eval(Cont ainer,"DataItem.FieldValue"))
%>'>
</asp:Label>
However, after some research, I found that I did see the Output
parameter in the Selecting event, but only in the
SqlDataSourceStatusEventArgs object. Finally, it occured to me, I
would simply set the Output parameter for the SqlDataSource in the
Selecting event handler.

protected void SQLDataSource5_Selected(
object sender,
SqlDataSourceStatusEventArgs e)
{
SqlDataSource5.SelectParameters["ValueName"].DefaultValue =
e.Command.Parameters["@ValueName"].Value.ToString();
}

I guess it was built this way to allow the developer to handle datatype
conversions in the Selected event handler and parameter setup in the
Selecting event handler.

Then when I was back in my lookup function, I could access the value of
the output paramter.

string valuename =
SqlDataSource5.SelectParameters["ValueName"].DefaultValue;

An alterative may have been to add the data source to the GridRow
ItemTemplate...however, since I was using an asp:label, I didn't see a
method to bind the output parameter to the label.
Mar 30 '07 #1
1 12269
I'm not sure this will help you. You have a lot of words, and it's late
where I live. Here's an example of how to use output parameters to get data
from SQLServer. This runs against Northwind.
How to get a parameter back:

Dim SQLString = "SELECT @UnitPrice = UnitPrice, " & _
" @UnitsInStock = UnitsInStock, " & _
"FROM Products WHERE ProductName = @ProductName"

Dim pUnitPrice, pInStock, pProductName As SqlParameter
pUnitPrice = cmd.Parameters.Add("@UnitPrice", SqlDbType.Money)
pUnitPrice.Direction = ParameterDirection.Output
pInStock = cmd.Parameters.Add("@UnitsInStock", SqlDbType.NVarChar, 20)
pInStock.Direction = ParameterDirection.Output
pProductName = cmd.Parameters.Add("ProductName", SqlDbType.NvarChar, 40)
pProductName.Value = "Chai"

cmd.ExecuteNonQuery()
if pUnitPrice.Value Is DBNull.Value Then 'none were found
Console.WriteLine("No product found named {0}", pProductName.Value)
Else
Console.WriteLine("Unit price: {0}", pUnitPrice.Value)
Console.WriteLine("In Stock: {0}", pInStock.Value)
End If

Robin S.
--------------------------------
"John Bailo" <ja*****@texeme.comwrote in message
news:Uo******************************@speakeasy.ne t...
>
This is a my solution to getting an Output parameter from a
SqlDataSource.

I have seen a few scant articles but none of them take it all the way to
a solution. Hopefully this will help some poor soul.

Situation: I want to do a lookup using a stored procedure for each value
in a Row within a GridView.

I use a lookup function in my code behind, evaluating the necessary bound
fields. The problem is the SqlDataSource representing the stored
procedure was returning an empty string after running the .Select()
method.

<asp:Label
ID="FieldValue"
runat="server" Text='<%#
lookupValueName((string)DataBinder.Eval(Container, "DataItem.FieldName"),(string)DataBinder.Eval(Cont ainer,"DataItem.FieldValue"))
%>'>
</asp:Label>
However, after some research, I found that I did see the Output parameter
in the Selecting event, but only in the SqlDataSourceStatusEventArgs
object. Finally, it occured to me, I would simply set the Output
parameter for the SqlDataSource in the Selecting event handler.

protected void SQLDataSource5_Selected(
object sender,
SqlDataSourceStatusEventArgs e)
{
SqlDataSource5.SelectParameters["ValueName"].DefaultValue =
e.Command.Parameters["@ValueName"].Value.ToString();
}

I guess it was built this way to allow the developer to handle datatype
conversions in the Selected event handler and parameter setup in the
Selecting event handler.

Then when I was back in my lookup function, I could access the value of
the output paramter.

string valuename =
SqlDataSource5.SelectParameters["ValueName"].DefaultValue;

An alterative may have been to add the data source to the GridRow
ItemTemplate...however, since I was using an asp:label, I didn't see a
method to bind the output parameter to the label.

Apr 1 '07 #2

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

Similar topics

1
by: Sandie Towers | last post by:
We use a number of similar databases and frequently create a new database using a backup restore of another similar database. We try to keep changes between databases in _Additional tables - like...
5
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as...
8
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it...
4
by: Mr Not So Know It All | last post by:
im new to SQL Server and ASP.Net. Here's my problem. I have this SQL Server stored procedure with an input parameter and output parameter CREATE PROCEDURE . @in_rc varchar(8) @out_eList...
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. ...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
7
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) {...
1
by: Mike Lester | last post by:
I have a need for a stored procedure to return a recordset AND an output parameter that contains the count of records in the recordset. I can get either but not both. (ie. if there is a select...
8
by: Mike P | last post by:
How do you return a datareader from a stored procedure, but also return an output parameter? Here is my code, which is just returning a data reader at the moment : _conn.Open(); SqlCommand...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.