I am using a SqlDataSource to bind DetailsView for userprofile, which allows users to view their information and at the same time change their particulars. (select, retrieve and update)
however, this error occured– A field or property with the name ‘Username’ was not found on the selected data source when I click the hyperlink to direct to the “userprofile” page.
Exception Details: System.Web.HttpException: A field or property with the name 'Username' was not found on the selected data source.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
this is the code inside userprofile.aspx file:
<asp:DetailsView ID="UserProfile" runat="server"
AutoGenerateRows="False" DataKeyNames="UserId"
DataSourceID="UserProfileDataSource" DefaultMode="Edit"
onitemupdated="UserProfile_ItemUpdated">
<Fields>
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="HomeTown" HeaderText="HomeTown"
SortExpression="HomeTown" />
<asp:BoundField DataField="HomepageUrl" HeaderText="HomepageUrl"
SortExpression="HomepageUrl" />
<asp:BoundField DataField="Signature" HeaderText="Signature"
SortExpression="Signature" />
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="UserProfileDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>"
SelectCommand="SELECT * FROM [UserProfiles] WHERE ([UserId] = @UserId)"
onselecting="UserProfileDataSource_Selecting"
UpdateCommand="UPDATE UserProfiles SET
Username = @Username,
HomeTown = @HomeTown,
HomepageUrl = @HomepageUrl,
Signature = @Signature WHERE UserId = @UserId ">
<SelectParameters>
<asp:Parameter Name="UserId" Type="Object" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="HomeTown" />
<asp:Parameter Name="HomepageUrl" />
<asp:Parameter Name="Signature" />
<asp:Parameter Name="UserId" />
</UpdateParameters>
</asp:SqlDataSource>
this is the code behind userprofile.aspx.cs :
protected void UserProfileDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
// Get a reference to the currently logged on user
MembershipUser currentUser = Membership.GetUser();
// Determine the currently logged on user's UserId value
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
// Assign the currently logged on user's UserId to the @UserId parameter
e.Command.Parameters["@UserId"].Value = currentUserId;
}
EDIT
I had change the SQL query to:
SelectCommand= "SELECT [aspnet_Membership].UserId, [HomeTown], [HomepageUrl], [Signature] FROM [UserProfiles] INNER JOIN [aspnet_Membership] ON aspnet_Membership.UserId = UserProfiles.UserId WHERE aspnet_Membership.UserId=@UserId"
onselecting="UserProfileDataSource_Selecting"
UpdateCommand="UPDATE UserProfiles SET
Username = @Username,
HomeTown = @HomeTown,
HomepageUrl = @HomepageUrl,
Signature = @Signature WHERE UserId = @UserId ">
But the error still appear:
A field or property with the name ‘Username’ was not found on the selected data source
Check the field names in the UserProfiles table. It appears you don’t have a Username field. It is good practice to explicitly state the fields you want to SELECT rather than using the
SELECT * FROM ..syntax.