I have a SqlDataAdapter with a SqlCommandBuilder attached to it.
public SqlDataAdapter SelectNone(string Table)
{
SqlDataAdapter result = new SqlDataAdapter();
SqlCommandBuilder builder = new SqlCommandBuilder(result);
builder.ConflictOption = ConflictOption.OverwriteChanges;
builder.SetAllValues = true;
result.SelectCommand = new SqlCommand(String.Format("SELECT * FROM {0} WHERE RowID IS NULL", Table), Connection);
result.SelectCommand.Parameters.AddWithValue("TableName", Table);
//I do this because I read the existence of this parameter in my Fill function,
//and set the TableName of the DataTable to the filled table.
return result;
}
I pull a table out of the database (RowID in my database is never null so I get a blank table), put some validated values in it via some bound textboxes, and put it back in.
This is how I'm going from a blank DataTable to a new DataRowView to bind to:
TableAdapter = Database.SelectNone(Resources.tableLicenseSet);
BoundTable = Database.GetDataTable(TableAdapter);
//GetDataTable calls Fill on a new blank DataTable using the TableAdapter
Guid NewRowID = Guid.NewGuid();
DataRow NewRow = BoundTable.NewRow();
NewRow[Resources.colLicenseSetID] = NewRowID;
BoundTable.Rows.Add(NewRow);
DataRowView BinderObject = BoundTable.DefaultView[0];
SetBinder.DataSource = BinderObject; //SetBinder is a BindingSource on my control
And this is how I'm binding:
foreach (DataColumn column in BoundTable.Columns)
{
if (!Controls.ContainsKey("in" + column.ColumnName)) continue;
Controls["in" + column.ColumnName].DataBindings.Add(new Binding("Text", SetBinder, column.ColumnName) {
DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
});
}
The data columns that aren't exposed to the UI are bound to properties I create on the control. The binding works a treat, values propagate from my UI to the DataTable.
The problem is, when I put it back in the database, the command that's generated is only attempting to put the primary key and the fields that are null in my DataRowView into the database, even if the SqlCommandBuilder is told to SetAllValues. The most infuriating thing is that I have another control where I'm using the same calls, and there it works, and if I call GetInsertCommand() on my SqlCommandBuilder, I get a valid INSERT statement that will insert all my specified values.
I'm not entirely sure why this is going on. Does anyone know why this would be happening?