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:
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:
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!


Reply With Quote