Pregunta

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;
}
¿Fue útil?

Solución 3

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">

Otros consejos

I think you are not really updating your table with your code.

Change this part of your code here:

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("SELECT * FROM Incidents ORDR BY IncidentId");

    DataSet updatedIncidentsDataSet = new DataSet();
    myAdapter.Fill(updatedIncidentsDataSet, "Incidents");

    myAdapter.UpdateCommand = myCommand;
    myAdapter.Update(updatedIncidentsDataSet, "Incidents");

    return updatedIncidentsDataSet;
}

You need to specify the tableName that you are updating. In your case that is Incidents

Try:

myAdpter.Update(updatedIncidentsDataSet, "Incidents");

If we don't specify the table name that we are updating the DataAdapter will use the default table name which is "Table" that's why you are getting Update unable to find TableMapping['Table']

Hi I am getting few things from your question that on first load you are getting what you want (desired result). Then you are doing something (perhaps adding some row, editing or deleting) and you know which operation you are doing on which row.

Then why don't you write a separate function to perform that activity separately. If you are updating a new row then update it in DB and re-pouplate your grid from database.

    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;
    myCommand.ExecuteNonQuery();

this will save data into your DB table and refill the grid from your base query. In this way you will get your desired result.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top