I am trying to set up a program that, when a user updates an item description, will update the database upon the save of the form. I have set up everything I have found that is required and yet the adapter never updates. It selects and inserts great but never update. Code as follows:
internal void UpdateDB(DataTable Items)
{
using ( var ItemsAdapter = new SqlDataAdapter("select * from dbo.Items", Properties.Settings.Default.ConnectionString) )
using ( var ItemsCB = new SqlCommandBuilder(ItemsAdapter) )
{
ItemsAdapter.UpdateCommand = ItemsCB.GetUpdateCommand(true);
ItemsAdapter.InsertCommand = ItemsCB.GetInsertCommand(true);
ItemsAdapter.DeleteCommand = ItemsCB.GetDeleteCommand(true);
ItemsAdapter.AcceptChangesDuringUpdate = true;
foreach ( DataRow Row in Items.AsEnumerable() )
{
if ( !_Items.TableContains("Item", Row["Item"]) )
{ Row.AcceptChanges(); Row.SetAdded(); }
else if ( _Items.TableContains("Item", Row["Item"]) )
{ Row.AcceptChanges(); Row.SetModified(); }
}
ItemsAdapter.Update(Items);
_LoadAll();
}
}
The .TableContains()
extension is a home brew extension I built to check against the table to see if a value exists. It is running perfect and sets rows where the PK exists already to modified and rows where the PK does not exist to Added.
public static bool TableContains(this DataTable DT, string ColumnName, object ItemtoCheck)
{
return DT.AsEnumerable()
.Select(r => r.Field<string>(ColumnName))
.ToList()
.FindIndex(x => x.Equals(ItemtoCheck.ToString(), StringComparison.OrdinalIgnoreCase)) != -1;
}
The _LoadAll();
method is just a method to reload all the DataTables
after the changes have been made to the database.