I have a gridview and inside of that gridview I have a few templatefields that are editable
which works, but when I update the row, it only sends the original value, not my updated value to the rowupdating method
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
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