Gridview onUpdate set new value

Posted on 2006-05-16
Last Modified: 2010-08-05
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">
                  <asp:Label ID=lblPlayer1 Width="95%" runat="server" Text='<%# Eval("vcPlayer1") %>'/>
                  <asp:TextBox ID=txtPlayer1 Width="95%" runat="server" Text='<%# Bind("vcPlayer1") %>'/>
                  <asp:Label ID=lblLogin1 Width="95%" runat="server" Text='<%# Eval("vcLogin1") %>'/>

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

           <asp:ControlParameter ControlID="gvComp" Name="iCompId" PropertyName="SelectedValue" Type="Int32" />
            <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" />

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

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.

Question by:Poidda
    LVL 16

    Accepted Solution

    Modify the e.NewValues["vcLogin1"] value.  
    It has copied the GridView values to the NewValues collection and will use the NewValues collection to do the update.

    e.NewValues["vcLogin1"]  = Context.User.Identity.Name;
    LVL 1

    Author Comment

    thanks.  i worked this out.

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

    points are yours!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now