Select Parameters in SQL data source

Discussion in 'ASP .Net' started by Alan, Aug 28, 2006.

  1. Alan

    Alan Guest

    Hi,
    I'm having a problem inserting a parameter into an SQL Select statement
    with <asp:ControlParameter>. This should be very simple, but for some
    reason it just isn't working:

    <%-- Get username and put it in the textbox --%>
    <% Session("UserName") =
    HttpContext.Current.User.Identity.Name.ToString%>
    <% UserField.Text = Session("UserName")%>

    <%-- Display the data --%>
    <asp:GridView ID="GridView1" runat="server"
    DataSourceID="SqlDataSource1" />

    <%-- Set up the data source --%>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:dConnect %>"
    ProviderName="<%$ ConnectionStrings:dConnect.ProviderName
    %>" SelectCommand="SELECT a, b FROM x WHERE username = ?">
    <SelectParameters>
    <asp:ControlParameter ControlID="UserField"
    PropertyName="Text" Type="string" />
    </SelectParameters>
    </asp:SqlDataSource>

    <%-- textbox --%>
    <asp:textbox ID="UserField" runat="server"/>

    If I hardcode the username into the Select statement, or if I set the
    DefaultValue of the ControlParameter, it works ok, but I need to be
    able to insert the username of whoever's logged on at the time. I'm
    using ODBC and MySQL.

    If anyone has any ideas on this I'd be very grateful.

    Cheers,

    Alan
    Alan, Aug 28, 2006
    #1
    1. Advertising

  2. Alan

    KJ Guest

    Include an asp:HiddenField on the page. Configure the ODS to use that
    hiddenfield control as source for the input parameter. Before
    databinding or calling Select on the ODS, set the Value of the hidden
    field. The ODS will use the value from the hiddenfield.

    Alan wrote:
    > Hi,
    > I'm having a problem inserting a parameter into an SQL Select statement
    > with <asp:ControlParameter>. This should be very simple, but for some
    > reason it just isn't working:
    >
    > <%-- Get username and put it in the textbox --%>
    > <% Session("UserName") =
    > HttpContext.Current.User.Identity.Name.ToString%>
    > <% UserField.Text = Session("UserName")%>
    >
    > <%-- Display the data --%>
    > <asp:GridView ID="GridView1" runat="server"
    > DataSourceID="SqlDataSource1" />
    >
    > <%-- Set up the data source --%>
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:dConnect %>"
    > ProviderName="<%$ ConnectionStrings:dConnect.ProviderName
    > %>" SelectCommand="SELECT a, b FROM x WHERE username = ?">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="UserField"
    > PropertyName="Text" Type="string" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > <%-- textbox --%>
    > <asp:textbox ID="UserField" runat="server"/>
    >
    > If I hardcode the username into the Select statement, or if I set the
    > DefaultValue of the ControlParameter, it works ok, but I need to be
    > able to insert the username of whoever's logged on at the time. I'm
    > using ODBC and MySQL.
    >
    > If anyone has any ideas on this I'd be very grateful.
    >
    > Cheers,
    >
    > Alan
    KJ, Aug 29, 2006
    #2
    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. Harry Zoroc
    Replies:
    1
    Views:
    915
    Gregory Vaughan
    Jul 12, 2004
  2. Author Tarun Tyagi
    Replies:
    0
    Views:
    692
    Author Tarun Tyagi
    Dec 29, 2004
  3. Jason
    Replies:
    2
    Views:
    491
    Jonathan Mcdougall
    May 13, 2006
  4. tom c
    Replies:
    1
    Views:
    190
    PeterKellner
    Jun 8, 2006
  5. palmiere
    Replies:
    1
    Views:
    379
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page