Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:05:27+00:00 2026-06-02T04:05:27+00:00

I have been trying to use OleDbDataAdapter to update a DataTable but got confused

  • 0

I have been trying to use OleDbDataAdapter to update a DataTable but got confused about the commands.
Since I sometimes get info from diffrent tables I can’t use a CommandBuilder.
So I have tried to create the commands on my on but found it hard with the parameters.
DataTable.GetChanges returns rows that needs to use an INSERT or an UPDATE command – I guess I can’t distinct between them.
I need you to complete the following:

DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
// Here I create the SELECT command and pass the connection.
da.Fill(dt);
// Here I make changes (INSERT/UPDATE) to the DataTable (by a DataGridView).
da.UpdateCommand = new OleDbCommand("UPDATE TABLE_NAME SET (COL1, COL2, ...) VALUES (@newVal1, @newVal2, ...) WHERE id=@id"); // How can I use the values of the current row (that the da is updating) as the parameters (@newVal1, @newVal2, id....)?

Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:05:30+00:00Added an answer on June 2, 2026 at 4:05 am

    The data adapter can work in conjunction with the datatable. As such, I’ve actually wrapped mine together into a class and works quite well. Aside from the complexities of my stuff, here’s a snippet that might help you along. When adding a parameter, you can identify the column source that the data is coming from FROM the DataTable. This way, when a record is internally identified as “Added” or “Updated” (or “Deleted”), when you build your SQL Insert/Update/Delete commands, it will pull the data from the columns from the respective rows.

    For example. Say I have a DataTable, primary Key is “MyID” and has columns “ColX, ColY, ColZ”. I create my DataAdapter and build out my select, update, delete commands something like… (? is a place-holder for the parameters)

    DataAdapter myAdapter = new DataAdapter()
    
    myAdapter.SelectCommand = new OleDbCommand();
    myAdapter.InsertCommand = new OleDbCommand();
    myAdapter.UpdateCommand = new OleDbCommand();
    myAdapter.DeleteCommand = new OleDbCommand();
    
    myAdapter.SelectCommand.CommandText = "select * from MyTable where MyID = ?";
    myAdapter.InsertCommand.CommandText = "insert into MyTable ( ColX, ColY, ColZ ) values ( ?, ?, ? )";
    myAdapter.UpdateCommand.CommandText = "update MyTable set ColX = ?, ColY = ?, ColZ = ? where MyID = ?";
    myAdapter.DeleteCommand.CommandText = "delete from MyTable where MyID = ?";
    

    Now, each has to have their respective “Parameters”. The parameters have to be addded in the same sequence as their corresponding “?” place-holders.

    // Although I’m putting in bogus values for preparing the parameters, its just for
    // data type purposes. It does get changed through the data adapter when it applies the changes

    OleDbParameter oParm = new OleDbParameter( "myID", -1 );
    oParm.DbType = DbType.Int32;
    oParm.SourceColumn = "myID";  // <- this is where it looks back to source table's column
    oParm.ParameterName = "myID";  // just for consistency / readability reference
    
    myAdapter.SelectCommand.Parameters.Add( oParm );
    

    do similar for rest of parameters based on their types… char, int, double, whatever

    Again, I have like a wrapper class that handles managment on a per-table basis… in brief

    public myClassWrapper
    {
        protected DataTable myTable;
        protected DataAdapter myAdapter;
        ... more ...
    
        protected void SaveChanges()
        {
        }
    }
    

    Its more complex than just this, but during the “SaveChanges”, The datatable and dataAdapter are in synch for their own purposes. Now, flushing the data. I check for the status of the table and then you can pass the entire table to the dataAdapter for update and it will cycle through all changed records and push respective changes. You’ll have to trap for whatever possible data errors though.

    myAdapter.Update( this.MyTable );
    

    As it finds each “changed” record, it pulls the values from the Column Source as identified by the parameter that is found in the table being passed to the adapter for processing.

    Hopefully this has given you a huge jump on what you are running into.

    —- COMMENT PER FEEDBACK —-

    I would put your update within a try/catch, and step into the program to see what the exception is. The message adn/or inner exception of the error might give more info. However, try to simplify your UPDATE to only include a FEW fields with the WHERE “Key” element.

    Additionally, and I oopsed, missed this from first part answer. You might have to identify the datatable’s “PrimaryKey” column. To do so, its a property of the DataTable that expects and array of columns that represent the primary key for the table. What I did was…

    // set the primary key column of the table
    DataColumn[] oCols = { myDataTbl.Columns["myID"] };
    myDataTbl.PrimaryKey = oCols;
    

    I would comment out your full update string and all its parameters for your UPDATE. Then, build it with just as simple as my sample of only setting 2-3 columns and the where clause

    myAdapter.UpdateCommand.CommandText = "update MyTable set ColX = ?, ColY = ? where MyID=?";
    Add Parameter object for "X"
    Add Parameter object for "Y"
    Add Parameter object for "MyID"
    

    Pick fields like int or char so they have the least probability of problems for data type conversions, then, once that works, try adding all your “int” and “character” columns… then add any others. Also, which database are you going against. SOME databases don’t use “?” as placeholder in the command but use “named” parameters, some using

    "actualColumn = @namedCol"
    or even
    "actualColumn = :namedCol"
    

    Hope this gets you over the hump…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use JQuery UI but cant seem to get any
I have been trying to use regex but I cant seem to get it
I have been trying to use Prototype to do some DOM manipulation, but I
I have been trying to use an update trigger which checks for the title_id
I have been trying to use localstorage to store Jquery-min but I am unable
I have been trying to use some objective C, i have got as far
I have been trying to use nextAll() and siblings() but those functions never return
I have been trying to use JScrollPane with my applet, but it doesn't work.
I have been trying to use protobuf-net with MonoTouch but I have no idea
I installed AjaxControlToolkit 4.0 and have been trying to use the extenders, but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.