Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Receiving NoNullAllowedException setting SelectedValue from code.

Receiving NoNullAllowedException setting SelectedValue from code.

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brett Slaski
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • B 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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Either assign DBNull.Value if the DataColumn allows nulls, or make sure it's assigned. Using a typed DataSet can help lay out the DataSet, 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

      B 1 Reply Last reply
      0
      • H Heath Stewart

        Either assign DBNull.Value if the DataColumn allows nulls, or make sure it's assigned. Using a typed DataSet can help lay out the DataSet, 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

        B Offline
        B Offline
        Brett Slaski
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • B 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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 the Hashtable may invalidate that if the number of buckets must be increased to handle the capacity, of course). Any IList implement defines IndexOf, which can help. That doesn't mean, of course, that it will work (you might get an NotSupportedException), but every IList implementation I've ever worked with does define this method to do what it should.

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups