locked
BindingSource AddNew Contraint BUG? RRS feed

  • Question

  • This is my problem.

    I have a table in Access "ASV Letter" in which the field "Number" is the primary key. I connect to that DataBase with a DataSet.

    I have a button to add a letter. It opens a DialogForm where the user types the values of the new field.

    If he tries a value that was already used, it trips an error as expected.

    But....

    If he gives a new letter and immediately after try again with the same number, no exception at all are raised! If he clicks the button again, the addNew method fires an exception (dbNull not allowed in the field).

    I really cannot uderstand why.

    Any one could help? 


    Private Sub buttonAddTransmittal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAddTransmittal.Click

    Me.ASVLetterBindingSource.AddNew()

    Me.ASVLetterBindingSource.Current.ROW("Date") = Today

    DialogForNewASVLetter.ShowDialog()

    If DialogForNewASVLetter.DialogResult = Windows.Forms.DialogResult.OK Then

    Try

    Me.ASVLetterBindingSource.EndEdit()

    Catch myException As System.Data.NoNullAllowedException

    MsgBox(myException.Message & "Probably, your missed typed the number that shall be numeric.")

    Me.ASVLetterBindingSource.CancelEdit()

    Me.ASVLetterBindingSource.EndEdit()

    Me.ASVLetterBindingSource.RemoveCurrent()

    Catch myException As System.Data.ConstraintException

    MsgBox(myException.Message & "Probably, the letter already exists.")

    Me.ASVLetterBindingSource.CancelEdit()

    Catch myException As System.Exception

    MsgBox("kk")

    End Try

    Me.ASV_LetterTableAdapter.Update(Me.Document_ListDataSet.Tables("ASV Letter"))

    End If

    End Sub


    Thursday, August 24, 2006 7:03 PM

Answers

All replies

  • I am not sure. Some one could tell me?
    Friday, August 25, 2006 8:29 AM
  • You haven't posted the code that shows the interaction between the dialog and the main form... the problem probably lies in the way your editing the record...
    Friday, August 25, 2006 8:33 PM
    Moderator
  • I have paste the code here in below. The Bindings Count is because I do not dispose of the form. One of my suprise, is why BindingSource.EndEdit does not update the base unless the DataAdapter calls the update method.

    Shall I make a separate command? But, in this case, the DataSet will immediately update all the controls bound to him?

     

    But, in any case, as a matter of understanding and curisoity, why it does not work?

     

    Imports System.Windows.Forms

    Public Class DialogForNewASVLetter

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

    Me.DialogResult = System.Windows.Forms.DialogResult.OK

    Me.Close()

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click

    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel

    Me.Close()

    End Sub

    Private Sub DialogForNewASVLetter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If Me.TextBox1.DataBindings.Count = 0 Then

    Me.TextBox1.DataBindings.Add("text", Form1.ASVLetterBindingSource, "Number")

    Me.DateTimePicker1.DataBindings.Add("Value", Form1.ASVLetterBindingSource, "Date")

    End If

    End Sub

    End Class

    Thursday, August 31, 2006 7:15 AM
  • EndEdit() just ends the current edit operation on the current row in the binding source.  This ends the edit of the dataset.  No changes are sent back to the database until the Update() method of the TableAdapter is called.

    I'm not entirely sure what's going on here, but it probably is not a good idea to databind to a BindingSource through the default instance of a form.  The dialog should probably just show, let the user enter values and then close.  After you return to the main form you can then read the values in the textbox and datatimepicker and attempt to update your BindingSource.Current datarow.  

    Binding to the BindingSource in the main form from the dialog doesn't seem necessary and may be causing your strange results.

    Here's an example mod to your code:

    Private Sub buttonAddTransmittal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAddTransmittal.Click

    Me.ASVLetterBindingSource.AddNew()

    Me.ASVLetterBindingSource.Current.ROW("Date") = Today

    DialogForNewASVLetter.ShowDialog()

    If DialogForNewASVLetter.DialogResult = Windows.Forms.DialogResult.OK Then

    Try

    Me.ASVLetterBindingSource.Current.Row("Date") = DialogForNewASVLetter.DateTimePicker1.Value
    Me.ASVLetterBindingSource.Current.Row("Number") = DialogForNewASVLetter.TextBox1.Text

    Me.ASVLetterBindingSource.EndEdit()

    Catch myException As System.Data.NoNullAllowedException

    MsgBox(myException.Message & "Probably, your missed typed the number that shall be numeric.")

    Me.ASVLetterBindingSource.CancelEdit()

    Me.ASVLetterBindingSource.EndEdit()

    Me.ASVLetterBindingSource.RemoveCurrent()

    Catch myException As System.Data.ConstraintException

    MsgBox(myException.Message & "Probably, the letter already exists.")

    Me.ASVLetterBindingSource.CancelEdit()

    Catch myException As System.Exception

    MsgBox("kk")

    End Try

    Me.ASV_LetterTableAdapter.Update(Me.Document_ListDataSet.Tables("ASV Letter"))

    End If

    End Sub

     

    The just remove the binding code from the Form_Load of the dialog.

    Thursday, August 31, 2006 6:52 PM
    Moderator
  • Thanks.

    I will try that. It seems simplet therefore better. I'll let you know.

    Friday, September 1, 2006 10:55 AM
  • ALL OK. Thanks.

    Friday, September 1, 2006 10:58 AM
  • Hi if you add the exact reply which is the answer for you then it will quick for the other user's of the forum to pick up the answer quickly. and here you have mark your own reply as answer.
    Friday, September 1, 2006 11:04 AM