Link to home
Start Free TrialLog in
Avatar of meyerc74
meyerc74

asked on

How to encrypt a password on FormView update and insert event using an EntityDataSource

I have a FormView with a textbox for a password.  The FormView is using an EntityDataSource as a data source an I am trying to apply a hash function the textbox value before it is entered into the database.

Formview:

<asp:FormView
      ID="fvUser"
      runat="server"
      DataKeyNames="id"
      DataSourceID="edsUserDetails"
      DefaultMode="ReadOnly"
      OnItemInserting="fvUser_ItemInserting"
      width="100%"
      >
        <EditItemTemplate>
              .
              .
              .
            <div class="label-Field">Password</div>
                <asp:TextBox ID="tbxPassword" runat="server" Width="200px" TextMode="Password" Text='<%# Bind("password") %>' />
         </EditItemTemplate>
</asp:FormView>

Entity Datasource:

<asp:EntityDataSource ID="edsUserDetails" runat="server"
                        ConnectionString="name=WomEntities"
                        DefaultContainerName="WomEntities"                         
                        EntitySetName="Users"
                        EnableUpdate="True"
                        EnableInsert="True"
                        OnInserted="edsUserDetails_ItemInserted"
                        OnUpdating="edsUserDetails_onUpdating"
                        AutoGenerateWhereClause="true">
                        <WhereParameters>
                              <asp:ControlParameter ControlID="fView$gvSearchResult" DbType="Int32" Name="id" PropertyName="SelectedValue" />
                        </WhereParameters>
                  </asp:EntityDataSource>

Code Behind:

protected void edsUserDetails_onUpdating( object sender, EntityDataSourceChangingEventArgs e ) {
                  TextBox ltb_password = ( TextBox )fvUser.FindControl( "tbxPassword" );
                  string lpswd = encrypt( ltb_password.Text );
                  e.Entity.password = lpswd;                  
            }

I found a blog that suggested the onUpdating method above but it is returning error:  "Error      3      'object' does not contain a definition for 'password' and no extension method 'password' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"

Any suggestions for this.  I simply want to use the existing code and interrupt the database insert/update to hash the password.

Thank you
ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meyerc74
meyerc74

ASKER

thank you NazoUK....i knew it was something simple i was overlooking.  Corrected code below:

protected void edsUserDetails_onUpdating( object sender, EntityDataSourceChangingEventArgs e ) {
                  TextBox ltb_password = ( TextBox )fvUser.FindControl( "tbxPassword" );
                  string lpswd = encrypt( ltb_password.Text );
                  User lentity = (User)e.Entity;
                  lentity.password = lpswd;
            }