I have a webform called UserProfile.aspx. After user logged-in, they can view and edit their particulars at UserProfile. I used SqlDatasource and was bind with DetailsView. In the detailsview, it retrieved data from 2 table; aspnet_Membership and Details table respectively.
Following is the code which I get the current logged-in user(inside UserProfile.aspx.cs):
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
MembershipUser currentUser = Membership.GetUser();
e.Command.Parameters["@UserId"].Value = currentUser.ProviderUserKey.ToString();
}
I am able to view the user's information, but when I edit the information and check in the database, all the record in the table was updated with the new values, instead of that particular row.
I had researched on this, and knowing that by stating the WHERE clause, only those records that satisfy the condition will have that attribute updated. I had add in the WHERE clause to my update statement, but it seem not to be working.
The following is the code in UserProfile.aspx:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"
SelectCommand="SELECT Details.UserId, aspnet_Membership.Password, aspnet_Membership.Email, Details.CustName, Details.CustNum, Details.CustRole, Details.CustStatus, Details.PName, Details.PEmail, Details.PRole, Details.WedDate FROM Details INNER JOIN aspnet_Membership ON Details.UserId = aspnet_Membership.UserId WHERE (Details.UserId = @UserId)"
onselecting="SqlDataSource1_Selecting"
UpdateCommand="UPDATE Details SET CustName = @CustName, CustNum = @CustNum, CustRole = @CustRole, CustStatus = @CustStatus, PName = @PName, PEmail = @PEmail, PRole = @PRole, WedDate = @WedDate WHERE [UserId] = @UserId">
<SelectParameters>
<asp:Parameter Name="UserId" type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" type="String" />
<asp:Parameter Name="CustName" type="String" />
<asp:Parameter Name="CustNum" type="String" />
<asp:Parameter Name="CustRole" type="String" />
<asp:Parameter Name="CustStatus" type="String" />
<asp:Parameter Name="PName" type="String" />
<asp:Parameter Name="PEmail" type="String" />
<asp:Parameter Name="PRole" type="String" />
<asp:Parameter Name="WedDate" type="String" />
<asp:Parameter Name="Password" type="String" />
<asp:Parameter Name="Email" type="String" />
</UpdateParameters>
</asp:SqlDataSource>