Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > why do Update method of SqldataAdapter not work?
 

why do Update method of SqldataAdapter not work?

This is my coding, DeleteComand and InsertComand work, but UpdateComand not work, if change dap.AcceptChangesDuringFill is true, then UpdateComand work, but Delete and Insert not work,   please help me to solve this problem as impossible as
         
            DataSet dsLocal = new DataSet();
            DataSet dsServer = scsiClass.getDS("SELECT * FROM [t_HR_Group]", "t_HR_Group");

            SqlDataAdapter dap = new SqlDataAdapter("SELECT * FROM [t_HR_Group1]", con);
            dap.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            dap.AcceptChangesDuringFill = false;

            dap.UpdateCommand = cmd.GetUpdateCommand();
            dap.DeleteCommand = cmd.GetDeleteCommand();
            dap.InsertCommand = cmd.GetInsertCommand();

            dap.Fill(dsLocal, "t_HR_Group");
            SqlCommandBuilder cmd = new SqlCommandBuilder(dap);

            dsServer.Merge(dsLocal, true, MissingSchemaAction.AddWithKey);
            //dsServer.GetChanges();

            int cn = dsServer.Tables[0].Rows.Count - 1;
            for (int i = 0; i <= cn; i++)
            {
                DataRow dr = dsServer.Tables[0].Rows[i];
                if (dr.RowState == DataRowState.Unchanged)
                {
                    dr.SetAdded();
                }
                else
                    if (dr.RowState == DataRowState.Added)
                    {
                        dr.AcceptChanges();
                        dr.Delete();
                    }
            }
          
                     
            int eff=dap.Update(dsServer.Tables[0]);
  • Moved byHarry ZhuMSFTWednesday, October 07, 2009 8:20 AMrelating to sqldataadapter and databinding (From:Visual C# General)
  •  
BRUCE LAM  Tuesday, October 06, 2009 3:43 AM
Hi BRUCE,

The following code is not necessary.

dap.UpdateCommand = cmd.GetUpdateCommand();
dap.DeleteCommand = cmd.GetDeleteCommand();
dap.InsertCommand = cmd.GetInsertCommand();

When you create SqlCommandBuilder (SqlCommandBuilder cmd = new SqlCommandBuilder(dap);), it already does the work.

I don’t know what the purpose for this code is.
int cn = dsServer.Tables[0].Rows.Count - 1;
            for (int i = 0; i <= cn; i++)
            {
                DataRow dr = dsServer.Tables[0].Rows[i];
                if (dr.RowState == DataRowState.Unchanged)
                {
                    dr.SetAdded();
                }
                else
                    if (dr.RowState == DataRowState.Added)
                    {
                        dr.AcceptChanges();
                        dr.Delete();
                    }
            }
Based on my understanding, you just need to call “int eff=dap.Update(dsServer.Tables[0]);”, it will do all the insert, update, delete work for you. Could you please test my suggestion and tell me the result?

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  20 hours 45 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• can i copy row and it's child rows and make new row with the copied one ?
• Seelcting more than one row in a DataGrid
• Please Help!!! BindingSource.Filter
• Have I accidentally created multiple currency managers?
• Question on how to handle pending updates
• Aggregate on Filtered Child Relations Rows [C# - .NET 2.0]
• ListBox.DataSource = causes SelectedIndex = 0 and I need SelectedIndex = -1
• DataGridView detecting change in combo box cell
• Combobox's behavior in dataGridView
• bind cusom object to datagrid not working