Click here to Skip to main content
15,895,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to update, delete and insert data through data set and data adapter. Here is a patch of code for inserting the data. Can anyone help me on updating and deleting the data in the Access database?

C#
DataRow oOrderRow = oDS.Tables["Orders"].NewRow();
oOrderRow["CustomerName"] = "Customer ABC";
oOrderRow["ShippingAddress"] = "ABC street, 12345";
oDS.Tables["Orders"].Rows.Add(oOrderRow);
  
DataRow oDetailsRow = oDS.Tables["OrderDetails"].NewRow();
oDetailsRow["ProductId"] = 1;
oDetailsRow["ProductName"] = "Product 1";
oDetailsRow["UnitPrice"] = 1;
oDetailsRow["Quantity"] = 2;
oDetailsRow.SetParentRow(oOrderRow);
oDS.Tables["OrderDetails"].Rows.Add(oDetailsRow);
 
oOrdersDataAdapter.Update(oDS, "Orders");
oOrderDetailsDataAdapter.Update(oDS, "OrderDetails");
conn.Close();
Posted
Updated 12-Apr-11 12:04pm
v3

First you must find particular row, one way or another. I'll assume you know row's ID (PK) or index in table and that you don't use typed dataset.

//find your row by ID
DataRow myRow = oDS.Orders.Rows.Find(rowID);


or

//find your row by index
DataRow myRow = oDS.Orders.Rows[index];


//similar thing can be done with typed dataSet, consider this as pseudo-code because I don't know all names.
YourDataSetName.OrdersRow myRow = oDS.Orders.FindByOrdersID(rowID);


Now you can manipulate with row as you wish.

//deleting  
myRow.Delete();


or

//editing
myRow["CustomerName"]= "New name";
//etc.


When you are done call the adapter's Update method like you do after insert.

oOrdersDataAdapter.Update(oDS);


Just play with it, I've learned, so can you :)!
 
Share this answer
 
I shall use SqlCommandBuilder for the purpose of Record insertion and deletion for your code. After adding new row (with records) to your table add following code :
int NofRecs_Added = 0; // to indicate a records has been added
int NofRecs_Edited = 0;// to indicate a records has been edited 

oDS.Tables["Orders"].Rows.Add(oOrderRow); // Your line of code
NofRecs_Added++;   // record added indication
               // do this for NofRecs_Edited also (in case any record edited)

 // Now adding records to SQL Database
    SqlCommandBuilder sqbld = new SqlCommandBuilder(oOrdersDataAdapter);
    try
    {
        if (oDS.HasChanges() == true)
        {
           if (NofRecs_Added >= 1) 
           {
             // AddDataSet and EditDataSet to declared earlier like your oDS                   with Same DataAdapter and table to oDS dataset
               AddDataSet = oDS.GetChanges(DataRowState.Added); 
               oOrdersDataAdapter.Update(AddDataSet, "Orders");
           }
     // For deletion     
     if (NofRecs_Edited >= 1)  
     {
        EditDataSet = oDS.GetChanges(DataRowState.Deleted);
        oOrderDataAdapter.Update(EditDataSet, "Orders");
     }

        oDS.AcceptChanges();
   }
}
catch (Exception s)
{
    MessageBox.Show(s.Message);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900