Combo Box SelectedValue property returns NULL on newly inserted rows
Hi everyone,
I have a data bound ComboBox control on my form which is initialized as follows:
Database db = Database.Instance;
m_accountsList.DataSource = db.Students.Tables[0];
m_accountsList.DisplayMember = "Name";
m_accountsList.ValueMember = "ID";
The dataset is edited down the line to insert a new row as follows:
OleDbDataAdapter adapter = db.CreateAdapterForTable("Students_T");
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
DataRow row = db.Students.Tables[0].NewRow();
row["Name"] = m_Name.Text.Trim();
db.Students.Tables[0].Rows.Add(row);
adapter.Update(db.Students.GetChanges());
db.Students.AcceptChanges();
I see my ComboBox get updated as soon as the second code block is executed. However, selecting the newly inserted entry results in an error as the returned SelectedValue property is null (It is supposed to return the ID) of the newly inserted row.
Has anyone else noticed the same thing? Any suggestions on how to fix this would be extremely appreciated.
Thanks,
xarg
[1213 byte] By [
xargon] at [2007-11-20 10:55:16]

# 2 Re: Combo Box SelectedValue property returns NULL on newly inserted rows
Hey,
What is the best way to refresh a data source?
The whole point of sharing thesame DataSet with all the different UI controls was that I do not have to do all this when different UI elements edit the underlying data source.
Seems like a bug to me...It updates the DisplayValue but not the SelectedValue. I mean why should it return one column and not the other?
xarg
xargon at 2007-11-9 11:37:09 >

# 3 Re: Combo Box SelectedValue property returns NULL on newly inserted rows
The DisplayValue shows in the combobox because you are writing it to the bound dataset which is on the client. If you also explicitly write the ID, It'll work as is. However, that may not be a good idea for many reasons.
There are many ways to refresh datasource depending on how you are loading it in the first place, I'd look into the db.Students.Load method
# 5 Re: Combo Box SelectedValue property returns NULL on newly inserted rows
I see my ComboBox get updated as soon as the second code block is executed. However, selecting the newly inserted entry results in an error as the returned SelectedValue property is null (It is supposed to return the ID) of the newly inserted row.
Hookay. Given that you never seem to set the ID in code, I presume that the database is calculating the ID for you? So here's how your code works:
You add a new row to the original datatable.
You call GetChanges which returns you a whole, brand new datatable with all the changed rows cloned into it, from the original table
You uPdate() that, and the databsae calcs the ID, and the ID gets set in the cloned table
You throw the coned table away, call acceptchanges on the original table..
And now are left with a NULL ID
Did you see what happened? :)
cjard at 2007-11-9 11:40:13 >
