EDN Admin
Well-known member
I am taking a Visual Basic class and we are working with updating databases. We are using the authors database in Pubs.mdf. There are 8 text fields and 1 checkbox. Everything was working fine, until I used DataBinding with the checkbox. Here is an example of my code that occurs during the form loading:<br/> <br/> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br/> Try<br/> Set up and fill dataset<br/> APubsDataSet = New PubsDataSet<br/> AnAuthorsTableAdapter = New PubsDataSetTableAdapters.authorsTableAdapter<br/> AnAuthorsTableAdapter.Fill(APubsDataSet.authors)<br/> ADataTable = APubsDataSet.Tables(0)<br/> <br/> <br/> Set up the binding source<br/> AnAuthorsBindingSource = New BindingSource<br/> With AnAuthorsBindingSource<br/> .DataSource = Me.APubsDataSet<br/> .DataMember = "authors"<br/> .MoveLast()<br/> .MoveFirst()<br/> End With<br/> <br/> Bind controls<br/> Au_idTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "au_id")<br/> Au_lnameTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "au_lname")<br/> Au_fnameTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "au_fname")<br/> PhoneTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "phone")<br/> AddressTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "address")<br/> CityTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "city")<br/> StateTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "state")<br/> ZipTextBox.DataBindings.Add("text", AnAuthorsBindingSource, "zip")<br/> ContractCheckBox.DataBindings.Add("checked", AnAuthorsBindingSource, "contract") <br/> SetControlsReadOnly(True)<br/> Catch ex As Exception<br/> MessageBox.Show("Data Error: Unable to load database.", "Error")<br/> End Try<br/> <br/> ToolStripStatusLabel2.Text = String.Empty<br/> <br/> End Sub<br/> <br/> I currently have the ContractCheckBox commented out and it allows me to "add a record", meaning I click the add button and it creates a new record, but I cant save it because it wont allow the Contract checkbox to be NULL. The real fun begins when I run the program without that line commented out. With the line not commented out, when I click the add button, it changes the record that I am currently on, meaning that all the read only text fields for lets 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<br/> <br/> Additional information: Column au_id does not allow nulls.<br/> <br/> It also highlights .AddNew() in the code for my AddSaveButton_Click procedure:<br/> <br/> Private Sub AddSaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddSaveButton.Click<br/> Begin an Add operation or cancel the current operation.<br/> <br/> If AddSaveButton.Text = "&Add" Then<br/> With AnAuthorsBindingSource<br/> .EndEdit()<br/> .AddNew() <br/> End With<br/> AddingBoolean = True<br/> Au_idTextBox.Focus()<br/> SetNavigation(False)<br/> SetControlsReadOnly(False)<br/> SetButtonsForEdit()<br/> Else<br/> Save button clicked<br/> Try<br/> AnAuthorsBindingSource.EndEdit()<br/> AnAuthorsTableAdapter.Update(APubsDataSet.authors)<br/> ToolStripStatusLabel2.Text = "Record Saved"<br/> AddingBoolean = False<br/> EditingBoolean = False<br/> SetNavigation(True)<br/> SetControlsReadOnly(True)<br/> ResetButtonsAfterEdit()<br/> Catch ex As Exception<br/> Catch duplicate records and constraint violations<br/> MessageBox.Show(ex.Message)<br/> End Try<br/> End If<br/> End Sub<br/> <br/> <br/> I really have no idea what I am doing wrong. My textbook doesnt even have an explanation. If anyone needs me to post the full code then I can definitely do that. Any help would be greatly appreciated. Thanks!<br/> <br/> Aren Anderson
View the full article
View the full article