Link to home
Start Free TrialLog in
Avatar of Poidda
Poidda

asked on

Gridview onUpdate set new value

Hi,  I am using a gridview and want to set a field in the database to log that a change has been made.  

Here is a trimmed down version of my code

<asp:GridView ID=gvGroup runat="server" OnRowUpdating="gvGroup_OnRowUpdating"
        DataKeyNames="iGroupId" AutoGenerateColumns=false DataSourceID="SqlDataSource_gvGroup">
   <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                  <asp:Label ID=lblPlayer1 Width="95%" runat="server" Text='<%# Eval("vcPlayer1") %>'/>
            </ItemTemplate>
            <EditItemTemplate>
                  <asp:TextBox ID=txtPlayer1 Width="95%" runat="server" Text='<%# Bind("vcPlayer1") %>'/>
                  <asp:Label ID=lblLogin1 Width="95%" runat="server" Text='<%# Eval("vcLogin1") %>'/>
            </EditItemTemplate>
      </asp:TemplateField>
   </Columns>
<asp:GridView>



<asp:SqlDataSource ID="SqlDataSource_gvGroup" runat="server" "
      SelectCommand="spGetGroup_CompId" SelectCommandType="StoredProcedure"
      UpdateCommand="spUpdateGroup" UpdateCommandType="StoredProcedure"
      DeleteCommand="spDeleteGroup" DeleteCommandType="StoredProcedure">

      <SelectParameters>
           <asp:ControlParameter ControlID="gvComp" Name="iCompId" PropertyName="SelectedValue" Type="Int32" />
      </SelectParameters>
      
        <UpdateParameters>
            <asp:Parameter Name="iGroupId" Type="Int32" />
            <asp:SessionParameter Name="iCompId" SessionField=CompId Type=Int32/>
            <asp:Parameter Name="dtGroupTime" Type="DateTime"/>
            <asp:Parameter Name="iHoleNo" Type="Int32" />
            <asp:Parameter Name="vcPlayer1" Type="String" />
            <asp:Parameter Name="vcLogin1" Type="String" />
            <asp:Parameter Name="vcPlayer2" Type="String" />
            <asp:Parameter Name="vcLogin2" Type="String" />
            <asp:Parameter Name="vcPlayer3" Type="String" />
            <asp:Parameter Name="vcLogin3" Type="String" />
            <asp:Parameter Name="vcPlayer4" Type="String" />
            <asp:Parameter Name="vcLogin4" Type="String" />
            <asp:Parameter Name="iRetVal" DefaultValue=0 Type="int32" />
      </UpdateParameters>

      <DeleteParameters>
            <asp:Parameter Name="iGroupId" Type="Int32" />
      </DeleteParameters>
</asp:SqlDataSource>


Now I thought I could set the lblLogin1 by checking on the RowUpdate like this.

    protected void gvGroup_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string strNewPlayer;
        string strOldPlayer;

        strNewPlayer = e.NewValues["vcPlayer1"] == null ? string.Empty : e.NewValues["vcPlayer1"].ToString();
        strOldPlayer = e.OldValues["vcPlayer1"] == null ? string.Empty : e.OldValues["vcPlayer1"].ToString();
        if (strNewPlayer != strOldPlayer)
        {
            Label lblLogin1 = (Label)gvGroup.Rows[e.RowIndex].FindControl("lblLogin1");
            lblLogin1.Text = Context.User.Identity.Name;
        }
     }

So this is all executed, but my database is not updated with the correct LoginId.  Im guessing that the update to te database is carried ou before gvGroup_OnRowUpdating is executed.  My question is:

How can I do this?

Cheers for your help in advance.

ASKER CERTIFIED SOLUTION
Avatar of RobertRFreeman
RobertRFreeman
Flag of United States of America 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 Poidda
Poidda

ASKER

thanks.  i worked this out.

e.NewValues.Add("vcLogin1", Context.User.Identity.Name);

points are yours!