Results 1 to 9 of 9

Thread: [RESOLVED] Dataadapter.Update not working..

  1. #1

    Thread Starter
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Resolved [RESOLVED] Dataadapter.Update not working..

    Hello...

    I know there are enough threads regarding this subject. But I didn't find any help with those. Thats why raising again over here..

    Im having a very strange problem.. I just copied the source code from one working application to this new application. It was working fine over there but not over here in new application. Neither it was showing any error nor updating the data. Below is the source code (method) that im using ..
    Code:
    public static void AppendViolations(string dsPath, DataTable dtSource, string PartitionID)
            {
                string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + dsPath;
                string SqlStatement = "select * from IncaViolations";
    
                OleDbConnection cn = new OleDbConnection(ConnectionString);
                OleDbCommand cmd = new OleDbCommand(SqlStatement, cn);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.AcceptChangesDuringFill = true;  //no use even if i try false also
                OleDbCommandBuilder cmdbuild = new OleDbCommandBuilder(da);
                DataSet ds = new DataSet();
                da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                da.Fill(ds, "IncaViolations");
                foreach (DataRow dr in dtSource.Rows)
                {
                    try
                    {
                        ds.Tables["IncaViolations"].ImportRow(dr);
                    }
                    catch (Exception ex)
                    {
                        ErrorLog = ErrorLog + ex.Message.ToString() + Environment.NewLine;
                        CompleteLog = CompleteLog + ex.Message.ToString() + Environment.NewLine;
                    }
    
                }
    
                da.InsertCommand = cmdbuild.GetInsertCommand();
                da.UpdateCommand = cmdbuild.GetUpdateCommand();
                da.DeleteCommand = cmdbuild.GetDeleteCommand();
    
                try
                {
                    int i=da.Update(ds, "IncaViolations");
    
                    System.Windows.Forms.MessageBox.Show(i.ToString());
                    CompleteLog = CompleteLog + "No Of Voilations Updated in " + PartitionID + " : " + ds.Tables["IncaViolations"].Rows.Count + Environment.NewLine;
                }
                catch (Exception ex1)
                {
    
                    ErrorLog = ErrorLog + ex1.Message.ToString() + Environment.NewLine;
                    CompleteLog = CompleteLog + ex1.Message.ToString() + Environment.NewLine;
                }
    
                // trans.Commit();
    
            }
    Can some body help in figure out the problem.

    Thank you,
    Srikanth.
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dataadapter.Update not working..

    First up, get rid of these lines because they're pointless:
    Code:
                da.InsertCommand = cmdbuild.GetInsertCommand();
                da.UpdateCommand = cmdbuild.GetUpdateCommand();
                da.DeleteCommand = cmdbuild.GetDeleteCommand();
    As for the question, I have no idea why you're Filling a new DataTable and importing its rows into an existing DataTable and then saving the existing DataTable. That's not going to make any changes to the existing DataTable that can then be saved. Is that what you're expecting to happen? I think you need to explain to us how this is supposed to work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Re: Dataadapter.Update not working..

    Well .. let me explain you my goal..

    I need to Import some records from one access DB to another Access db, which are completly identical interms of its fields and keys.

    and coming to my code, Im taking the path of the destination db, and the data to update (in a datatable) from source db as parameters and looping through all the records from source table and importing rows in to destination table.

    After importing all the rows, Im using update command of dataadapter to update the newly imported rows.

    I hope Im doing the correct way only.. Is there any alternate and best way for my goal ??

    However, thanks for quick response.

    Thank you.
    Srikanth
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dataadapter.Update not working..

    What you're doing is kinda correct but this is the problem:
    csharp Code:
    1. da.AcceptChangesDuringFill = true;
    You're doing the exact opposite of what you need to do. AcceptChangesDuringFill is true by default and you need to set it to false. What happens is this. When you call Fill the adapter reads the data from the database and, for each record, it adds a new DataRow to the DataTable. To begin with, all those new rows have a RowState of Added, indicating that they are ready to be inserted. That is exactly what you want. If AcceptChangesDuringFill is false then it stops there. If it's true however, it will call AcceptChanges on the DataTable and all the RowStates get changed to Unchanged. Unchanged rows get ignored when you call Update.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Re: Dataadapter.Update not working..

    Sorry !!!No use !!! even with
    Code:
    da.AcceptChangesDuringFill = false;
    ...
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dataadapter.Update not working..

    I don't know exactly what you're doing wrong but here's the simplest way to transfer data from one database to another. There's one Datatable, one DataAdapter and two Connections:
    csharp Code:
    1. using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT statement here",
    2.                                                        "source connection string here"))
    3. {
    4.     adapter.InsertCommand = new OleDbCommand("INSERT statement here",
    5.                                              new OleDbConnection("destination connection string here"));
    6.     adapter.AcceptChangesDuringFill = false;
    7.  
    8.     // Add parameters to InsertCommand here.
    9.  
    10.     DataTable table = new DataTable();
    11.  
    12.     adapter.Fill(table);
    13.     adapter.Update(table);
    14. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Re: Dataadapter.Update not working..

    Thats cool..

    Buy how do we write the insert statement that will insert multiple records ??

    Do you want me to loop through each record??

    --Srikanth
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dataadapter.Update not working..

    Quote Originally Posted by nagasrikanth View Post
    Buy how do we write the insert statement that will insert multiple records ??
    Go to the Database Development forum and read the FAQ. You'll find links to threads that will show you how.
    Quote Originally Posted by nagasrikanth View Post
    Do you want me to loop through each record??
    Did I loop through each record?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Re: Dataadapter.Update not working..

    i got that...

    I found that, the row state of the newly addded rows are showing as "Unchanged" only. Hence I have removed that ImportRow Method and placed the following code..
    Code:
    DataRow drow = ds.Tables[0].NewRow();
                        drow.ItemArray=dr.ItemArray;
                        ds.Tables[0].Rows.Add(drow);
    It worked..

    However, Thanks for your wonderful support "jmcilhinney".

    -Srikanth
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width