how to pass variable to SelectCommand of SqlDataSource?

Discussion in 'ASP .Net' started by phil, Jun 2, 2006.

  1. phil

    phil Guest

    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
     
    phil, Jun 2, 2006
    #1
    1. Advertising

  2. phil

    Brock Allen Guest

    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>


    -Brock
    http://staff.develop.com/ballen


    > 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
     
    Brock Allen, Jun 2, 2006
    #2
    1. Advertising

  3. phil

    phil Guest

    Thanks, it works ...

    "Brock Allen" <> wrote in message
    news:...
    > 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>
    >
    >
    > -Brock
    > http://staff.develop.com/ballen
    >
    >
    > > 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

    >
    >
     
    phil, Jun 2, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tammy B
    Replies:
    0
    Views:
    753
    Tammy B
    Mar 9, 2006
  2. John Kotuby
    Replies:
    1
    Views:
    883
    John Kotuby
    Feb 24, 2007
  3. Dave

    Selectcommand Variable

    Dave, May 14, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    1,587
    =?Utf-8?B?U2l2YSBN?=
    May 16, 2007
  4. Fred Dag

    In-line code in <asp:SqlDataSource SelectCommand tag

    Fred Dag, Aug 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    352
    Fred Dag
    Aug 24, 2005
  5. Martin
    Replies:
    1
    Views:
    284
    Martin
    Mar 28, 2007
Loading...

Share This Page