Results 1 to 1 of 1

Thread: Can Not Add a New Record in VB with a Checkbox DataBinding

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    1

    Can Not Add a New Record in VB with a Checkbox DataBinding

    I am taking a Visual Basic class and we are working with updating databases. We are using the authors database in Pubs.mdf which is a free download from Microsoft. There are 8 text fields and 1 checkbox for contract. When I click the add button, it edits the record that I am currently on, meaning that all the read only text fields for let's say Record 5 of 23 are able to be edited instead of creating a new record. I can click the cancel button and then if I click the add button again, VB blows up: An unhandled exception of type 'System.Data.NoNullAllowedException' occurred in System.Data.dll
    Additional information: Column 'au_id' does not allow nulls.
    It also highlights .AddNew() in the code for my AddSaveButton_Click procedure

    Here is an example of my code that occurs during the form loading:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         Try             'Set up and fill dataset             APubsDataSet = New PubsDataSet             AnAuthorsTableAdapter = New PubsDataSetTableAdapters.authorsTableAdapter             AnAuthorsTableAdapter.Fill(APubsDataSet.authors)              'Set up the binding source             AnAuthorsBindingSource = New BindingSource             With AnAuthorsBindingSource                 .DataSource = Me.APubsDataSet                 .DataMember = "authors"                 .MoveLast()                 .MoveFirst()             End With              'Bind controls             Au_idTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "au_id")             Au_lnameTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "au_lname")             Au_fnameTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "au_fname")             PhoneTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "phone")             AddressTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "address")             CityTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "city")             StateTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "state")             ZipTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "zip")             ContractCheckBox.DataBindings.Add("checked", AnAuthorsBindingSource, "contract")             SetControlsReadOnly(True)         Catch ex As Exception             MessageBox.Show("Data Error: Unable to load database.", "Error")         End Try          ToolStripStatusLabel2.Text = String.Empty      End Sub

    Here is the Add/Save Button code:

    vb Code:
    1. Private Sub AddSaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddSaveButton.Click         'Begin an Add operation or cancel the current operation.          If AddSaveButton.Text = "&Add" Then             With AnAuthorsBindingSource                 .EndEdit()                 .AddNew()             End With             AddingBoolean = True             Au_idTextBox.Focus()             SetNavigation(False)             SetControlsReadOnly(False)             SetButtonsForEdit()         Else             'Save button clicked             Try                 AnAuthorsBindingSource.EndEdit()                 AnAuthorsTableAdapter.Update(APubsDataSet.authors)                 ToolStripStatusLabel2.Text = "Record Saved"                 AddingBoolean = False                 EditingBoolean = False                 SetNavigation(True)                 SetControlsReadOnly(True)                 ResetButtonsAfterEdit()             Catch ex As Exception                 'Catch duplicate records and constraint violations                 MessageBox.Show(ex.Message)             End Try         End If     End Sub

    Any help would be appreciated. If I comment out the ContractCheckBox DataBindings Line, the AddSaveButton works just fine, as does the DeleteCancelButton. I just can't actually save the record because the contract check box is null. I can post more of the code if it would help!
    Last edited by si_the_geek; Mar 17th, 2010 at 04:16 AM. Reason: corrected code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width