Check box data binding, AddNew doesn't work

I am having problems with binding to a check box. It works fine until I
attempt to add a new record to the dataset through the binding context
object.

The code that adds the new record to the dataset is listed below
Me.BindingContext(dsDataSet, TableName).EndCurrentEdit()
Me.BindingContext(dsDataSet, TableName).AddNew()

The add new statement generates an error and a new record is not shown. Is there any solution


Answer this question

Check box data binding, AddNew doesn't work

  • Richard Petkiewicz

    Thank you Safa. I realized that I should go to knowledge base first. Thanks again.
  • michael olson

    I had the same problem as discused above.


    Try this:

    Dim sql As String = "Select CountryName,IsFirstCountry from tblCountry where Country= -1"
    Dim sa As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
    Dim ds As New DataSet

    Try
     sa.Fill(ds, "Country")   ' returns an empty dataset but with the correct structure
     Dim dr As DataRow = ds.Tables(0).NewRow   ' creates a new blank row
     ' ******* 
     dr.Item(1) = 0 ' 1 is the column with the check box
     ' *******
     ds.Tables(0).Rows.Add(dr)   ' add the blank row to the dataset
    Finally
     sa.Dispose()
    End Try
                
    The binding part will be :

    Me.txtCountry.DataBindings.Add(New Binding("Text",ds,"Country.CountryName"))
    Me.CheckBox1.DataBindings.Add(New Binding("Checked", ds, "Country.IsFirstCountry"))

    The trick is to first fill the "IsFirstCountry" field before binding the "CheckBox1" to the "IsFirstCountry" table field.

    I hope this will help!


           

  • welikeike

    Remember that they've re-organized intrinsic collections in .NET so that they are all 0 based.

    So in your 7 record example (6 existing +1 new), the count property is 7 - but these are records 0 thru 6.  Try Count-1 instead of just Count:

    chkApproval.DataBindings.Clear() 
    Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit() 
    Me.BindingContext(Ds, "eBackToProvider").AddNew() 
    Ds.Tables("eBackToProvider").Rows(Me.BindingContext(Ds, "eBackToProvider").Count-1).Item("Approval") = 0 
    Ds.Tables("eBackToProvider").AcceptChanges() 
    chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval") 

    Does that fix it

  • Ryan Jones


    Hi

    I got the ssame problem then after searching this site i found an easy solution..

    I recommend you to visit The knowledge base articles 326440 and 321504 titled as follows:

    BUG: AddNew Method of CurrencyManager Fails with Bound CheckBox

    BUG: Check Box Is Not Cleared When You Call AddNew() Method on a DataSet

    The problem and its solution is found there


    God luck


  • geman

    Yes, it is "Bit" type.

    As you advised I added the following after AddNew record:

    Ds.Tables("eBackToProvider").Rows(Me.BindingContext(Ds, "eBackToProvider").Count).Item("Approval") = 0
    Ds.Tables("eBackToProvider").AcceptChanges()

    So, codes like the following:

    chkApproval.DataBindings.Clear() 
    Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit() 
    Me.BindingContext(Ds, "eBackToProvider").AddNew() 
    Ds.Tables("eBackToProvider").Rows(Me.BindingContext(Ds, "eBackToProvider").Count).Item("Approval") = 0
    Ds.Tables("eBackToProvider").AcceptChanges()
    chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval") 

    But counting rows - Me.BindingContext(Ds, "eBackToProvider").Count - caused an error. Suppose I currently have 6 records. Error message is "There is no row at position 7."

    After AddNew VB counts 7 rows but when assigning values it does not recognize 7th row.

  • gregger

    i have seen the code, dear you haven't speecified the datafieldtext property, do go for that rest of the code is just fine

  • Jonas Rosqvist

    What is the specific error that is generated   Which of these two lines produces the error   Can you include the code you use to bind your checkbox

    I implemented these same two lines of code successfully in a Button.Click event in a test project.  I had a datagrid bound to my DataSet, if you can describe the binding you use for your checkbox, I can test that as well.

  • KyleB

    I'd have to double check, but I think that when you call the AddNew method, the position in the dataset is moved to the new record.

    You might try something like the following:
    Me.BindingContext(Ds, "eBackToProvider").AddNew() 
    Me.BindingContext(Ds, "eBackToProvider").Current.Item("Approval") = 0 

    It would probably be better to create an actual BindingManagerBase objec to work with throughout the routine.  Start your code with:

    Dim BMB as BindingManagerBase = Me.BindingContext(Ds, "eBackToProvider")

    and then:

    BMB.AddNew()
    BMB.Current.Item("Approval")=0

    I'm really not sure beyond that...  It has to be an issue with the way you are data bound.  The checkbox should not*space
    space*the AddNew method.  Try to edit your code so that all changes are done through the BindingManagerBase rather than through the DataSet.

  • Peter Mo.

    Unfortunately, no, that does not fix it. I used ----.Count-1 but error says "There is no row at position 6" I guess .AddNew command add "virtual" row which does not actually exists.

    I'm perplexed that this is a simple data entry form with check box binding. AddNew doesn't work with data binding check box  Microsoft was not aware of this  This is ridiculous. Have you ever seen data form with data bound check boxex

    Anyway, do you have any other idea  I'm desperate my project dead line is over. I have to solve this ASAP. Thank you for your any help.

  • Darin V

    You are adding a new record to the DataSet but not assigning it's fields any values before you rebind.  So your boolean "eBackToProvider.Approval" field is NULL.  This may be causing a problem when binding to the boolean "Checked" property...  Is the Approval field boolean in the datasource (or at least "Bit" type)

    First, suspend binding on your controls.  Then, add your new record and assign some default values to each field.  Call AcceptChanges method of the dataset and then resume binding.

    I may need to play with it a bit more... this seems easy enough... we are probably missing something simple...

  • SpaceGuy

    When the form loads check box "chkApproval" binds to my dataset "ds" table "eBackToProvider" field "Approval". Table "eBackToProvider" is in SQL server, "Approval" data type is bit.

    chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval")

    When I click "Add" button

    Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
    Me.BindingContext(Ds, "eBackToProvider").AddNew()
    ds_PositionChanged()

    I have:
    Private Sub ds_PositionChanged()
    lblNavLocation.Text = (((Me.BindingContext(Ds, "eBackToProvider").Position + 1).ToString + " of  ") _
    + Me.BindingContext(Ds, "eBackToProvider").Count.ToString)
    End Sub

    lblNavLocation.Text shows "1 of 'rows count + 1' but the form still shows old record not added new record. No error message.

    I found when there are check boxes bound to data AddNew doesn't work properly.

    So, I clear binding before adding a record as below

    chkApproval.DataBindings.Clear()
    chkApproval.Checked = False
    Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
    Me.BindingContext(Ds, "eBackToProvider").AddNew()
    ds_PositionChanged()

    It works fine showing new record on the form. But I have to re-bind after adding a record. So,

    chkApproval.DataBindings.Clear()
    chkApproval.Checked = False
    Me.BindingContext(Ds, "eBackToProvider").EndCurrentEdit()
    Me.BindingContext(Ds, "eBackToProvider").AddNew()
    ds_PositionChanged()
    chkApproval.DataBindings.Add("Checked", Ds, "eBackToProvider.Approval")

    Now I have an error message:

    "An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll 
    Additional information: Object cannot be cast from DBNull to other types."

    Can you help me

  • Check box data binding, AddNew doesn't work