?
Solved

Gridview onUpdate set new value

Posted on 2006-05-16
2
Medium Priority
?
1,189 Views
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">
   <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.

0
Comment
Question by:Poidda
2 Comments
 
LVL 16

Accepted Solution

by:
RobertRFreeman earned 2000 total points
ID: 16713697
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.

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

Author Comment

by:Poidda
ID: 16760353
thanks.  i worked this out.

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

points are yours!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 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