just getting started with my first db driven web project...
I am using a MySql database with membership and roles and got that working fine. I have tables containing details of courses that users are enrolled on etc. and want to display a list of courses for the user that is signed in, so he can continue lessons on the one of his choice.
How do I pass the users name to the database query for a DataList control. So far I have tried lots of variations of the following:
<asp:SqlDataSourceID="dsCourses"runat="server"ConnectionString="<%$ ConnectionStrings:xxx %>"ProviderName="<%$ ConnectionStrings:xxx.ProviderName %>"
SelectCommand="SELECT c.CourseName FROM courses c, enrolments e
WHERE c.CourseID=e.CourseID AND e.Username='<% =User.Identity.Name %>'">
</asp:SqlDataSource>
<asp:DataListID="DataList1"runat="server"DataSourceID="dsCourses">
<ItemTemplate>
<asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl="Lesson.aspx">'<%# Eval("CourseName") %>'</asp:HyperLink><br/>
</ItemTemplate>
</asp:DataList> </p>
However, the <% =User.Identity.Name %> doesn't work here (but it does elsewhere on the page) - not sure why?? The code works fine if I hard code a user name into the select statement.
Suggestions pleeeeeese!!
You need to use ProfileParameter for the SQL SelectParameters on SqlDataSource.
<asp:SqlDataSourceID="dsCourses"runat="server"ConnectionString="<%$ ConnectionStrings:xxx %>"
ProviderName="<%$ ConnectionStrings:xxx.ProviderName %>"
SelectCommand="SELECT c.CourseName FROM courses c, enrolments e
WHERE c.CourseID=e.CourseID AND e.Username=@.userName">
<SelectParameters>
<asp:ProfileParameter Name="userName" PropertyName="UserName" />
</SelectParameters>
</asp:SqlDataSource
where Name is the name of the parameter and PropertyName is the name of the profile property( current user)
Thanks
No comments:
Post a Comment