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 7778853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:30:09+00:00 2026-06-01T18:30:09+00:00

I try to use DataAdapter in C#.net. and still I do not understand something

  • 0

I try to use DataAdapter in C#.net. and still I do not understand something about DataAdapter.

I read many article and blogs about DataAdapter and DataReader.

I understood DataAdapter will open and close database automatically when they need.

But,

//conn.Open();
AdsDataAdapter da;
da = new AdsDataAdapter("Select * from Test", conn);
AdsCommandBuilder cb;
cb = new AdsCommandBuilder(da);

DataSet ds = new DataSet();
da.Fill(ds, "Test");

DataRow newrow = ds.Tables["Test"].NewRow();
newrow["Name"] = "How about";
ds.Tables["Test"].Rows.Add(newrow);
da.Update(ds, "Test");

When I run the code above, I get an error message that say “Connection must be open.”

Why the adapter can not open connection automatically?

and, I want to insert data using insertCommand (For this test, I opened the connection).

da.InsertCommand = new AdsCommand("INSERT INTO test (NAME) values('Insert Test #1')", conn);
//da.InsertCommand.ExecuteNonQuery(); // it works
da.Update(ds,"Test"); //but it does not works.

Many example using Adapter.Update(), but for me, it does not work 🙁

No error and nothing inserted.

and using da.InsertCommand.ExecuteNonQuery(); instead Update(), it works.

what am I doing wrong?

Thanks!

  • 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-01T18:30:10+00:00Added an answer on June 1, 2026 at 6:30 pm

    MSDN says that

    The Fill method implicitly opens the Connection that the DataAdapter
    is using if it finds that the connection is not already open. If
    Fill opened the connection, it will also close the connection when
    Fill is finished. This can simplify your code when dealing with a
    single operation such as a Fill or an Update.

    This means that after da.Fill(ds, "Test"); your connection is closed by the method itself. But you need it open for the following Update (and that fails)

    EDIT: Pseudocode derived from your code above

    using(AdsConnection com = new AdsConnection(connectionString));
    {
        conn.Open();
        using(AdsDataAdapter da = new AdsDataAdapter("Select * from Test", conn))
        {
            AdsCommandBuilder cb = new AdsCommandBuilder(da); 
            DataSet ds = new DataSet(); 
            da.Fill(ds, "Test"); 
    
            // Now the connection is still open and you can issue other commands
    
           DataRow newrow = ds.Tables["Test"].NewRow(); 
           newrow["Name"] = "How about"; 
           ds.Tables["Test"].Rows.Add(newrow); 
    
           // da.Update should work here. No more connection closed.
           da.Update(ds, "Test"); 
        }
    } // Exiting from the using block, the connection will be closed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I try use WCF with protobuf-net r.282 Ok. I mark my contracts with ProtoBehavior
I need tree file at my asp.net site. For getting icons I try use
I try use string as a regular expression pattern but I've following errors PHP
I try use a integer array in java with the code below: public static
I try to use the domainpeople.com API and to do I need to use
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I try to use the winDBG to debug a dump file. When I run
I try to use PHPMailer to send registration, activation. etc mail to users: require(class.phpmailer.php);
I try to use single quotes as much as possible and I've noticed that

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.