1

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;
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2665267
  • 25
  • 1
  • 7
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 24 '14 at 04:56

3 Answers3

0

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']

jomsk1e
  • 3,585
  • 7
  • 34
  • 59
  • No luck. I get the same result. It doesn't update and there is no error message. – user2665267 Apr 23 '14 at 00:55
  • You need to set a breakpoint inside the method I posted to see what you got in every line. – jomsk1e Apr 23 '14 at 03:30
  • At this line: myAdapter.Update(updatedIncidentsDataSet); I get Invalid Operation Exception: Update unable to find TableMapping['Table'] or DataTable 'Table'. I have tried adding a table to the dataset and then doing myAdapter.Update(updatedIncidentDataSet, "TableName") but this is not working. – user2665267 Apr 24 '14 at 00:51
  • I did try what you suggested in your edit. The error message is "Update unable to find TableMapping['Incidents'] or DataTable 'Incidents'." So I added updatedIncidentsDataSet.Tables.Add("Incidents"); and that still doesn't work. What am I missing? – user2665267 Apr 25 '14 at 00:16
  • Plese see the edited method in my answer. This time tested working from my end. – jomsk1e Apr 25 '14 at 02:29
0

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.

Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
0

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">
user2665267
  • 25
  • 1
  • 7