Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Gridview rowupdating problem

Gridview rowupdating problem

I have a gridview and inside of that gridview I have a few templatefields that are editable


<asp:TemplateField HeaderText="Subject/Description" SortExpression="SubjectDescription" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblSubjectDescriptione" runat="server" Text='<%# Eval("SubjectDescription") %>' />
    <EditItemTemplate >
        <asp:TextBox id="SubjectDescription" runat="server" Text='<%# Bind("SubjectDescription") %>'/>
<asp:TemplateField HeaderText="Responsible Person" SortExpression="ResponsiblePerson" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblResponsiblePerson" runat="server" Text='<%# Eval("ResponsiblePerson") %>' />
    <EditItemTemplate >
        <asp:TextBox id="ResponsiblePerson" runat="server" Text='<%# Bind("ResponsiblePerson") %>'/>
<asp:BoundField DataField="PriorityLevel" HeaderText="Priority Level" ReadOnly="True" HeaderStyle-Wrap="false" /> 
<asp:TemplateField HeaderText="Final Response Due Date" SortExpression="FinalResponseDueDate" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblFinalResponseDueDate" runat="server" Text='<%# Eval("FinalResponseDueDate", "{0:d}") %>' />
    <EditItemTemplate >
        <asp:TextBox id="FinalResponseDueDate" runat="server" Text='<%# Bind("FinalResponseDueDate", "{0:d}") %>'/>
<asp:TemplateField HeaderText="Completion Date" SortExpression="CompletionDate" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblCompletionDate" runat="server" Text='<%# Eval("CompletionDate", "{0:d}") %>' />
    <EditItemTemplate >
        <asp:TextBox id="CompletionDate" runat="server" Text='<%# Bind("CompletionDate", "0:d}") %>'/>

which works, but when I update the row, it only sends the original value, not my updated value to the rowupdating method


protected void gvCARs_RowUpdating(object sender, GridViewUpdateEventArgs e)
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["aero"].ToString()))
                    using (SqlCommand cmd = new SqlCommand("dbo.spx_UpdateCAR", conn))

                        cmd.Connection = conn;

                        //cmd.CommandText = "UPDATE Aerospace.dbo.CARLog SET SubjectDescription = @SubjectDescription, ResponsiblePerson = @ResponsiblePerson, CompletionDate = @CompletionDate WHERE CAR# = @CAR";

                        cmd.CommandType = CommandType.StoredProcedure;

                        // Get the Visa Index of the selected row.
                        HiddenField lbl = (HiddenField)gvCARs.Rows[e.RowIndex].FindControl("CARID");
                        string strSubjectDescription = ((TextBox)gvCARs.Rows[e.RowIndex].FindControl("SubjectDescription")).Text;
                        string strResponsiblePerson = ((TextBox)gvCARs.Rows[e.RowIndex].FindControl("ResponsiblePerson")).Text;
                        string strFinalResponseDueDate = ((TextBox)gvCARs.Rows[e.RowIndex].FindControl("FinalResponseDueDate")).Text;
                        string strCompletionDate = ((TextBox)gvCARs.Rows[e.RowIndex].FindControl("CompletionDate")).Text;
                        if (string.IsNullOrEmpty(strCompletionDate))
                            strCompletionDate = "none";
                        if (string.IsNullOrEmpty(strResponsiblePerson))
                            strResponsiblePerson = "none";
                        if (string.IsNullOrEmpty(strFinalResponseDueDate))
                            strFinalResponseDueDate = "none";
                        if (string.IsNullOrEmpty(strSubjectDescription))
                            strSubjectDescription = "none";

                        Response.Write("CAR: " + lbl.Value + "<BR>");
                        Response.Write("FinalResponseDueDate: " + strFinalResponseDueDate + "<br>");
                        Response.Write("ResponsiblePerson: " + strResponsiblePerson + "<br>");
                        Response.Write("Completion Date: " + strCompletionDate + "<br>");
                        Response.Write("Subject/Description: " + strSubjectDescription);

                        // Append the parameters.
                        cmd.Parameters.AddWithValue("@CAR", lbl.Value);
                        cmd.Parameters.AddWithValue("@SubjectDescription", strSubjectDescription);
                        cmd.Parameters.AddWithValue("@ResponsiblePerson", strResponsiblePerson);
                        cmd.Parameters.AddWithValue("@FinalResponseDueDate", strFinalResponseDueDate);
                        cmd.Parameters.AddWithValue("@CompletionDate", strCompletionDate);

                        // Open the connection.

                        // Execute the command.

            // NOTE: The nested using {... } directives in these methods will automatically close and dispose both Command and Connection objects.
            // It is the equivalent of wrapping the code in a try / finally block.

            // Exit edit mode.
            gvCARs.EditIndex = -1;

            // Rebind the GridView control to show data after updating.

            // Show the Add button.
            lbtnAdd.Visible = true;
            catch (Exception ex)
                Label1.Text = "Error in execution " + ex.ToString();

Thoughts? What am I missing? I think perhaps I have just been looking at this for too long and I am missing something obvious.


RE: Gridview rowupdating problem

Since you are not using the datasource controls(which is a good thing), the OldValues and NewValues collections will not be populated automatically.
So, here is some sample code that I use, and maybe it will help you:
This goes in your RowUpdating event


Dim cell As DataControlFieldCell = Nothing  'This type will change if you are using BoundFields
'The collection(s) have to be populated for EACH cell.
cell = Me.gvResults.Rows(e.RowIndex).Cells(0)
Me.gvResults.Columns(0).ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, True)
objSqlParam = New SqlParameter("@NewExpiredID", SqlDbType.Int)
objSqlParam.Value = e.NewValues("PropertyID_Expired") 'The parameter is case sensitive.

Repeat the process for each value you need to get. 

RE: Gridview rowupdating problem

Thank you, I will give this a shot and let you know how it goes!


RE: Gridview rowupdating problem

So, I missed one small piece in my Page_Load. I didn't check for page load. Now it works...

RE: Gridview rowupdating problem

Great, glad it is working for you

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close