Link to home
Start Free TrialLog in
Avatar of alexatsearidge
alexatsearidgeFlag for Canada

asked on

Updating a database using a datagrid

Hello,

I am using C# 1.1 and I have an app with a datagrid that is bound to a dataset.
I want the user to be able to alter the contents of the grid and click a button to update the database with the new data.

Is it necessary to define Update/Insert commands? Or can I do something simple like:
dataAdapter.Update(dataSet.GetChanges());
dataSet.AcceptChanges();
Avatar of alexatsearidge
alexatsearidge
Flag of Canada image

ASKER

Ok I've been looking up help and I see that I need to define the insert, update and delete commands for the data adapter.
Not exactly sure how to do that.

Also, I have about 10 different tables that this datagrid will display data for.  It depends what the user selects in a list box.
Does this mean that I have to have 10 different data adapaters and 10 sets of insert/update/delete commands?
nvm I got it.  How do I get a point refund?
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the post but I think I'll stick with my soultion.  Luckily I discovered the command builder so I do not have to deal with creating the Insert/Update/Delete commands

-------------------

// Create the DataSet
dsCDAConfig = new DataSet("RecordsFromProcedure");

// Define the stored procedure that will populate the dataset
OleDbCommand command= new OleDbCommand("pGetWhatever", conn);
command.CommandType = CommandType.StoredProcedure;                  
                                          
// Fill the Dataset with the return of the stored procedure
dataAdapter= new OleDbDataAdapter(command);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter);
dataAdapter.Fill(dataSet);       

// We want to return the view of the recordset
DataView dataView = dataSet.Tables[0].DefaultView;

-------- Since the command builder handles the update command I can do the following later

DataSet updatedDataSet = dsCDAConfig.GetChanges();
if (updatedDataSet == null)
{
  // nothing to update so we return
  return;
}

dataAdapter.Update(updatedDataSet); // The command builder creates the update command for me
dataAdapter.AcceptChanges();