I have a GridView and I would like to edit/update rows using a custom method in my ObjectDataSource. I can get this to work using auto-generated columns but not BoundFields which is what I need. I have tried doing GridView1.DataBind() in the RowUpdated, RowUpdating, and RowEditing events. I don't get an error message. It just doesn't update the row.
IncidentUpdate.aspx
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="IncidentID" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="ProductCode" HeaderText="Product Code" ReadOnly="True" />
<asp:BoundField DataField="DateOpened" HeaderText="Date Opened" ReadOnly="True" />
<asp:BoundField DataField="DateClosed" HeaderText="Date Closed" />
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" />
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetCustomerIncidents" TypeName="IncidentDB" UpdateMethod="UpdateIncident">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="customerID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="incidentID" Type="Int32" />
<asp:Parameter Name="dateClosed" Type="DateTime" />
<asp:Parameter Name="description" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
App_Code\Service.cs
public static DataSet UpdateIncident(int incidentID, DateTime dateClosed,string description)
{
TechSupportDB techSupportDB = new TechSupportDB();
var myConnection = techSupportDB.GetConnectionString();
SqlCommand myCommand = new SqlCommand();
myCommand.Parameters.AddWithValue("@IncidentID", incidentID);
myCommand.Parameters.AddWithValue("@DateClosed", dateClosed);
myCommand.Parameters.AddWithValue("@Description", description);
myCommand.CommandText =
"update Incidents set DateClosed = @DateClosed, Description = @Description where IncidentID = @IncidentID";
myCommand.Connection = myConnection;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet updatedIncidentsDataSet = new DataSet();
myAdapter.Fill(updatedIncidentsDataSet);
return updatedIncidentsDataSet;
}
The BoundField for IncidentID is set as readOnly so the user cannot edit this field but this also prevents it from passing the value necessary to do the update. Solution: set DataKeyNames = IncidentID in the GridView.
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2"
AutoGenerateColumns="False" DataKeyNames="IncidentID">