Receiving NoNullAllowedException setting SelectedValue from code.
-
I am having some trouble with setting a value to ComboBox.SelectedValue and then saving that value using DataBinding. Example: // There are five controls on a form, 1 ComboBox (comboBox), 2 TextBoxes (tbx1, tbx2) and 2 buttons (btnNew, btnSave) DataSet dsFillCombo; DataSet ds1; // I fill each DataSet using separate data adapters ... // The Combo is Bound like this comboBox.DataSource = dsFillCombo; comboBox.DisplayMember = "sectionIds.sectionName"; comboBox.ValueMember = "sectionIds.sectionId"; // So far, pretty basic. Now I bind the controls on the form to my other // DataSet tbx1.DataBinding.Add("Text", ds1, "users.fName"); tbx2.DataBinding.Add("Text", ds1, "users.lName"); comboBox.DataBinding.Add("SelectedValue", ds1, "users.sectionId"); BindingManagerBase bm = BindingContext[ds1, "users"]; // Still pretty basic, I could add more buttons to change the record // position, and the values // in the form controls would change with the data rows. // Now for my issue! // For the btnNew I want to add a new record to the dataset and set the // comboBox to the same value that it is currently. private void btnNew_Click(object sender, System.EventArgs e) { int i = Convert.ToInt32(this.comboBox.SelectedValue); bm.AddNew(); this.comboBox.SelectedValue = sid; } // Now I click the Save button and... private void btnSave_Click(object sender, System.EventArgs e) { bm.EndCurrentEdit(); } As soon as I click btn_Save an Exception is thrown: System.NoNullAllowedException: Column 'sectionId' does not allow nulls. ... If I actually select a new value using the comboBox then the new record is saved without an issue. Is there a way to set the SelectedValue of a ComboBox from code and have that value recongnized by DataBinding? Thank you for any assistance. Brett Slaski
-
I am having some trouble with setting a value to ComboBox.SelectedValue and then saving that value using DataBinding. Example: // There are five controls on a form, 1 ComboBox (comboBox), 2 TextBoxes (tbx1, tbx2) and 2 buttons (btnNew, btnSave) DataSet dsFillCombo; DataSet ds1; // I fill each DataSet using separate data adapters ... // The Combo is Bound like this comboBox.DataSource = dsFillCombo; comboBox.DisplayMember = "sectionIds.sectionName"; comboBox.ValueMember = "sectionIds.sectionId"; // So far, pretty basic. Now I bind the controls on the form to my other // DataSet tbx1.DataBinding.Add("Text", ds1, "users.fName"); tbx2.DataBinding.Add("Text", ds1, "users.lName"); comboBox.DataBinding.Add("SelectedValue", ds1, "users.sectionId"); BindingManagerBase bm = BindingContext[ds1, "users"]; // Still pretty basic, I could add more buttons to change the record // position, and the values // in the form controls would change with the data rows. // Now for my issue! // For the btnNew I want to add a new record to the dataset and set the // comboBox to the same value that it is currently. private void btnNew_Click(object sender, System.EventArgs e) { int i = Convert.ToInt32(this.comboBox.SelectedValue); bm.AddNew(); this.comboBox.SelectedValue = sid; } // Now I click the Save button and... private void btnSave_Click(object sender, System.EventArgs e) { bm.EndCurrentEdit(); } As soon as I click btn_Save an Exception is thrown: System.NoNullAllowedException: Column 'sectionId' does not allow nulls. ... If I actually select a new value using the comboBox then the new record is saved without an issue. Is there a way to set the SelectedValue of a ComboBox from code and have that value recongnized by DataBinding? Thank you for any assistance. Brett Slaski
Either assign
DBNull.Value
if theDataColumn
allows nulls, or make sure it's assigned. Using a typedDataSet
can help lay out theDataSet
, though it's all programmatic and nothing you can do manually (just much easier using the designer, plus it adds a lot of extran functionality for you as well - especially with .NET 2.0).Microsoft MVP, Visual C# My Articles
-
Either assign
DBNull.Value
if theDataColumn
allows nulls, or make sure it's assigned. Using a typedDataSet
can help lay out theDataSet
, though it's all programmatic and nothing you can do manually (just much easier using the designer, plus it adds a lot of extran functionality for you as well - especially with .NET 2.0).Microsoft MVP, Visual C# My Articles
It was suggested to me to use SelectedIndex instead of SelectedValue in my btnNew_Click method: private void btnNew_Click(object sender, System.EventArgs e) { int i = Convert.ToInt32(this.comboBox.SelectedValue); bm.AddNew(); this.comboBox.SelectedValue = sid; } >>>> Change to: private void btnNew_Click(object sender, System.EventArgs e) { int i = this.comboBox.SelectedIndex; bm.AddNew(); this.comboBox.SelectedIndex = sid; } Which this works great. I can preset the ComboBox to a value and it will be recognized by DataBinding, which isn't the case when you use SelectedValue. I just wonder how I would handle this if I don't know the index? If I have a langauge selector to default to the users preferred language, I know "EN" or "SP" but I don't know their index value. Is there a way to get this? Brett Slaski
-
It was suggested to me to use SelectedIndex instead of SelectedValue in my btnNew_Click method: private void btnNew_Click(object sender, System.EventArgs e) { int i = Convert.ToInt32(this.comboBox.SelectedValue); bm.AddNew(); this.comboBox.SelectedValue = sid; } >>>> Change to: private void btnNew_Click(object sender, System.EventArgs e) { int i = this.comboBox.SelectedIndex; bm.AddNew(); this.comboBox.SelectedIndex = sid; } Which this works great. I can preset the ComboBox to a value and it will be recognized by DataBinding, which isn't the case when you use SelectedValue. I just wonder how I would handle this if I don't know the index? If I have a langauge selector to default to the users preferred language, I know "EN" or "SP" but I don't know their index value. Is there a way to get this? Brett Slaski
There's always almost an index. Even in a
Hashtable
(which, in itself, doesn't use indexes but buckets), you can get an index of a key or value at that time (changing theHashtable
may invalidate that if the number of buckets must be increased to handle the capacity, of course). AnyIList
implement definesIndexOf
, which can help. That doesn't mean, of course, that it will work (you might get anNotSupportedException
), but everyIList
implementation I've ever worked with does define this method to do what it should.Microsoft MVP, Visual C# My Articles