Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview rowupdating problem

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
0
0
US
I have a gridview and inside of that gridview I have a few templatefields that are editable

Code:
<asp:TemplateField HeaderText="Subject/Description" SortExpression="SubjectDescription" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblSubjectDescriptione" runat="server" Text='<%# Eval("SubjectDescription") %>' />
    </ItemTemplate>
    <EditItemTemplate >
        <asp:TextBox id="SubjectDescription" runat="server" Text='<%# Bind("SubjectDescription") %>'/>
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Responsible Person" SortExpression="ResponsiblePerson" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblResponsiblePerson" runat="server" Text='<%# Eval("ResponsiblePerson") %>' />
    </ItemTemplate>
    <EditItemTemplate >
        <asp:TextBox id="ResponsiblePerson" runat="server" Text='<%# Bind("ResponsiblePerson") %>'/>
    </EditItemTemplate>
</asp:TemplateField>
<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}") %>' />
    </ItemTemplate>
    <EditItemTemplate >
        <asp:TextBox id="FinalResponseDueDate" runat="server" Text='<%# Bind("FinalResponseDueDate", "{0:d}") %>'/>
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Completion Date" SortExpression="CompletionDate" ItemStyle-Wrap="false">
    <ItemTemplate >
        <asp:Label ID="lblCompletionDate" runat="server" Text='<%# Eval("CompletionDate", "{0:d}") %>' />
    </ItemTemplate>
    <EditItemTemplate >
        <asp:TextBox id="CompletionDate" runat="server" Text='<%# Bind("CompletionDate", "0:d}") %>'/>
    </EditItemTemplate>
</asp:TemplateField>

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

Code:
        protected void gvCARs_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                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.
                        conn.Open();

                        // Execute the command.
                        cmd.ExecuteNonQuery();
                    }
                }

            // 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.
            BindGridView();

            // 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.

Thanks,
willie
 
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
Code:
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.
sqlparamCollection.Add(objSqlParam)

....
Repeat the process for each value you need to get.
 
Thank you, I will give this a shot and let you know how it goes!

Willie
 
So, I missed one small piece in my Page_Load. I didn't check for page load. Now it works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top