Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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):

C#
protected void SqlDataSource1_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
    // Assign the currently logged on user's UserId to the @UserId parameter
    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:

XML
<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>
Posted
Comments
DamithSL 1-Jul-12 5:09am    
like in SqlDataSource1_Selecting method where are you set UpdateParameters ?
mathidioticz 1-Jul-12 5:18am    
Hi, sorry what u mean by where I set the UpdateParameters?
DamithSL 1-Jul-12 5:41am    
In your grid view set DataKeyNames="UserId" property and check
mathidioticz 1-Jul-12 5:59am    
Hi, after I added in DataKeyNames="UserId" into the code, the error:
System.InvalidCastException: Object must implement IConvertible appeared.
Stack trace:
[InvalidCastException: Object must implement IConvertible.]
System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +3760921
System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +126
System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
DamithSL 1-Jul-12 6:18am    
in your all UpdateParameters you set type as string, may be that is the case, set correct type match with the database type for relevant field.

1 solution

In your grid view set DataKeyNames="UserId" property

and then remove type of UserId parameter

XML
<UpdateParameters>
      <asp:Parameter Name="UserId" />
      <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>
 
Share this answer
 
v2
Comments
mathidioticz 1-Jul-12 7:23am    
I added in as what you said, it still the same. All the records in the table had updated with the new value.
DamithSL 1-Jul-12 7:35am    
what is the primary key of Details table?
mathidioticz 1-Jul-12 8:39am    
I didn't set any primary key in Details table. as using UserId already make the row unique

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900