J
John Bailo
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(Container,"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.
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(Container,"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.