how to pass variable to SelectCommand of SqlDataSource?

P

phil

Hi,

I want to put a recordset in a gridview but i don't know how to pass the
value of the variable in the 'where' statement. The value of the variable is
set in the code-behind. i added a tag <selectParameters> but i don't know
which elements are needed and how...

This is my aspx file:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydb.mdb"
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name], [city] where [name]= @na">
<SelectParameters>
<asp:ControlParameter
Name="??" PropertyName="???" Type="String" ControlID="??" />
</SelectParameters>
</asp:SqlDataSource>

The code-behind contains this:
Public na As String 'i defined it public to be able to pass it to
aspx file ??
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
na = "phil"
End Sub

Thanks for help
Phil
 
B

Brock Allen

If the value of your parameter isn't able to be drawn automatically via one
of the built-in parameter types (like ControlParemeter, QueryStringParameter,
etc) you can always handle the SqlDataSOurce's pre-processing events such
as Selecting, Updating, Deleting and Inserting to manually change or add
a parameter to its SqlCommand.

<script runat="server">
protected void _data_Selecting(object sender, SqlDataSourceSelectingEventArgs
e)
{
e.Command.Parameters["bar"].Value = "oxox";
}
</script>

<asp:SqlDataSource runat="server" ID="_data"
SelectCommand="select * from foo where bar = @bar"
OnSelecting="_data_Selecting">
<SelectParameters>
<asp:parameter Name="bar" DefaultValue="xoxo" />
</SelectParameters>
</asp:SqlDataSource>
 
P

phil

Thanks, it works ...

Brock Allen said:
If the value of your parameter isn't able to be drawn automatically via one
of the built-in parameter types (like ControlParemeter, QueryStringParameter,
etc) you can always handle the SqlDataSOurce's pre-processing events such
as Selecting, Updating, Deleting and Inserting to manually change or add
a parameter to its SqlCommand.

<script runat="server">
protected void _data_Selecting(object sender, SqlDataSourceSelectingEventArgs
e)
{
e.Command.Parameters["bar"].Value = "oxox";
}
</script>

<asp:SqlDataSource runat="server" ID="_data"
SelectCommand="select * from foo where bar = @bar"
OnSelecting="_data_Selecting">
<SelectParameters>
<asp:parameter Name="bar" DefaultValue="xoxo" />
</SelectParameters>
</asp:SqlDataSource>




Hi,

I want to put a recordset in a gridview but i don't know how to pass
the value of the variable in the 'where' statement. The value of the
variable is set in the code-behind. i added a tag <selectParameters>
but i don't know which elements are needed and how...

This is my aspx file:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\mydb.mdb"
ProviderName="System.Data.OleDb"
SelectCommand="SELECT [name], [city] where [name]= @na">
<SelectParameters>
<asp:ControlParameter
Name="??" PropertyName="???" Type="String" ControlID="??" />
</SelectParameters>
</asp:SqlDataSource>
The code-behind contains this:
Public na As String 'i defined it public to be able to pass it
to
aspx file ??
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
na = "phil"
End Sub
Thanks for help
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top