Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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):
 
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:
 
<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 30-Jun-12 23:53pm
Comments
Damith Weerasinghe at 1-Jul-12 5:09am
   
like in SqlDataSource1_Selecting method where are you set UpdateParameters ?
junetanweiting at 1-Jul-12 5:18am
   
Hi, sorry what u mean by where I set the UpdateParameters?
Damith Weerasinghe at 1-Jul-12 5:41am
   
In your grid view set DataKeyNames="UserId" property and check
junetanweiting at 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
Damith Weerasinghe at 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.
junetanweiting at 1-Jul-12 6:29am
   
but my UserId was set as uniqueIdentifier..

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In your grid view set DataKeyNames="UserId" property
 
and then remove type of UserId parameter
 
          <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>
  Permalink  
v2
Comments
junetanweiting at 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.
Damith Weerasinghe at 1-Jul-12 7:35am
   
what is the primary key of Details table?
junetanweiting at 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)

  Print Answers RSS
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 289
2 CPallini 275
3 DamithSL 260
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100