Select Parameters in SQL data source

A

Alan

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
 
K

KJ

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top