1) Could anybody tell me why I am getting a NullReferenceException when I hit exit?
2) Also, how would I display my txtPrice in currency form ($0.00)
3) How would I disable my navigation buttons at the same time once they reach the end of the datagrid?

Option Strict On

Public Class frmBookInfo

Private aflag As Boolean
Private Sub All_TitlesBindingNavigator_RefreshItems(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub frmBookInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PublishersTableAdapter.Fill(Me._books_Fall2012_A2DataSet.Publishers)
'TODO: This line of code loads data into the '_books_Fall2012_A2DataSet.Publishers' table. You can move, or remove it, as needed.
Me.TitlesTableAdapter.Fill(Me._books_Fall2012_A2DataSet.Titles)
'TODO: This line of code loads data into the '_books_Fall2012_A2DataSet.Titles' table. You can move, or remove it, as needed.

  txtBookTitle.Focus()
  btnFirst.Enabled = False
  btnBack.Enabled = False
  btnSave.Enabled = False
  btnUndo.Enabled = False
  lblSelectPublisher.Visible = False
  cmbSelectPublisher.Visible = False
  ToolStripStatusLabel2.Text = "Record # " + (TitlesBindingSource.Position + 1).ToString + " out of " + TitlesBindingSource.Count.ToString
End Sub

Private Sub TitlesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TitlesBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me._books_Fall2012_A2DataSet)

End Sub

Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    TitlesBindingSource.MovePrevious()
    btnForward.Enabled = True
    btnLast.Enabled = True
    ToolStripStatusLabel2.Text = "Record # " + (TitlesBindingSource.Position + 1).ToString + " out of " + TitlesBindingSource.Count.ToString
    If TitlesBindingSource.Position = 0 Then
        btnBack.Enabled = False
    End If
End Sub

Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    TitlesBindingSource.MoveFirst()
    btnForward.Enabled = True
    btnLast.Enabled = True
    ToolStripStatusLabel2.Text = "Record # " + (TitlesBindingSource.Position + 1).ToString + " out of " + TitlesBindingSource.Count.ToString
    If TitlesBindingSource.Position = 0 Then
        btnFirst.Enabled = False
    End If
End Sub

Private Sub btnForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click
    btnBack.Enabled = True
    btnFirst.Enabled = True
    TitlesBindingSource.MoveNext()
    ToolStripStatusLabel2.Text = "Record # " + (TitlesBindingSource.Position + 1).ToString + " out of " + TitlesBindingSource.Count.ToString
    If TitlesBindingSource.Position = 215 Then
        btnForward.Enabled = False
    End If
End Sub

Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    btnBack.Enabled = True
    btnFirst.Enabled = True
    TitlesBindingSource.MoveLast()
    ToolStripStatusLabel2.Text = "Record # " + (TitlesBindingSource.Position + 1).ToString + " out of " + TitlesBindingSource.Count.ToString
    If TitlesBindingSource.Position = 215 Then
        btnLast.Enabled = False
    End If
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    aflag = True
    Me.TitlesBindingSource.AddNew()
    ToolStripStatusLabel2.Text = "Adding new row"
    ToolStripStatusLabel1.Visible = True
    add()

End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim results As DialogResult
    results = MessageBox.Show("Are you sure you want to delete this data?", "",
        MessageBoxButtons.YesNo)
    If results.Equals(DialogResult.Yes) Then
        TitlesBindingSource.RemoveCurrent()
        TitlesBindingSource.EndEdit()
    End If

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If txtBookTitle.Text = "" Then
        MessageBox.Show("Please enter a title")
        txtBookTitle.Focus()
    End If
    If txtISBN.Text = "" Then
        MessageBox.Show("Please enter a valid ISBN")
        txtISBN.Focus()
    End If
    If cmbSelectPublisher.SelectedIndex = -1 Then
        MessageBox.Show("Select a publisher")
        cmbSelectPublisher.Focus()
    End If
    If cmbSelectPublisher.SelectedIndex <> -1 Then
        TitlesBindingSource.EndEdit()

        Dim x As Integer
        x = CInt(cmbSelectPublisher.SelectedValue)

        _books_Fall2012_A2DataSet.Titles(TitlesBindingSource.Position).PubID = x
        TitlesTableAdapter.Update(_books_Fall2012_A2DataSet)
    End If
    Exit Sub

End Sub

Private Sub cmbSelectPublisher_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSelectPublisher.SelectedIndexChanged
    Dim intPubID As Integer
    intPubID = CInt(cmbSelectPublisher.SelectedValue)
    fillpub(intPubID)

End Sub

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    edit()

End Sub

Private Sub btnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUndo.Click
    undo()
    TitlesBindingSource.CancelEdit()

End Sub

Private Sub TitlesBindingSource_PositionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TitlesBindingSource.PositionChanged
    Dim fill As Integer
    If Not aflag Then

    ToolStripStatusLabel1.Text = ""
    ToolStripStatusLabel2.Text = "Record # " + (TitlesBindingSource.Position + 1).ToString + " out of " + TitlesBindingSource.Count.ToString

    fill = _books_Fall2012_A2DataSet.Titles(TitlesBindingSource.Position).PubID
    fillpub(fill)
    End If
End Sub


Private Sub fillpub(ByVal argPubID As Integer)

        Dim drpub As _books_Fall2012_A2DataSet.PublishersRow
        drpub = _books_Fall2012_A2DataSet.Publishers.FindByPubID(argPubID)

        If drpub.IsNameNull Then
            txtPublisher.Text = ""
        Else
            txtPublisher.Text = drpub.Name
        End If

        If drpub.IsAddressNull Then
            txtAddress.Text = ""
        Else
            txtAddress.Text = drpub.Address
        End If

        If drpub.IsCityNull Then
            txtCity.Text = ""
        Else
            txtCity.Text = drpub.City
        End If


        If drpub.IsStateNull Then
            txtState.Text = ""
        Else
            txtState.Text = drpub.State
        End If

        If drpub.IsZipNull Then
            txtZipCode.Text = ""
        Else
            txtZipCode.Text = drpub.Zip
        End If
    End Sub


Private Sub txtValidate_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPages.KeyPress, txtPublicationYear.KeyPress, txtPrice.KeyPress, txtISBN.KeyPress
        Select Case e.KeyChar
            Case ControlChars.Back

            Case "0"c To "9"c

            Case "-"c
                If sender Is txtPages Then
                    e.Handled = True
                ElseIf sender Is txtPublicationYear Then
                    e.Handled = True
                ElseIf sender Is txtPrice Then
                    e.Handled = True
                ElseIf CType(sender, TextBox).Text.Contains("-") Then
                    e.Handled = True
                Else
                End If
            Case "."c
                If sender Is txtPages Then
                    e.Handled = True
                ElseIf sender Is txtPublicationYear Then
                    e.Handled = True
                ElseIf sender Is txtISBN Then
                    e.Handled = True
                ElseIf CType(sender, TextBox).Text.Contains(".") Then
                    e.Handled = True
                  Else
                  End If
            Case "$"c
                If sender Is txtPages Then
                    e.Handled = True
                ElseIf sender Is txtPublicationYear Then
                    e.Handled = True
                ElseIf sender Is txtISBN Then
                    e.Handled = True
                ElseIf CType(sender, TextBox).Text.Contains("$") Then
                    e.Handled = True
                  Else
                  End If
            Case Else
                e.Handled = True
        End Select
    End Sub

Private Sub txtBookTitle_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBookTitle.GotFocus
  If txtBookTitle.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
      ToolStripStatusLabel1.Text = "Book title is required"
  End If

End Sub

Private Sub txtISBN_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtISBN.GotFocus
  If txtISBN.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
      ToolStripStatusLabel1.Text = "ISBN is required"
  End If
End Sub

Private Sub txtDescription_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDescription.GotFocus
  If txtDescription.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
      ToolStripStatusLabel1.Text = "Description is not required"
  End If
End Sub

Private Sub txtSubject_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSubject.GotFocus
  If txtState.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
      ToolStripStatusLabel1.Text = "Subject is not required"
  End If
End Sub

Private Sub txtPages_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPages.GotFocus
  If txtPages.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
      ToolStripStatusLabel1.Text = "Page # is not required"
  End If
End Sub

Private Sub txtPublicationYear_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPublicationYear.GotFocus
  If txtPublicationYear.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
  ToolStripStatusLabel1.Text = "Publication year is not required"
  End If
End Sub

Private Sub txtPrice_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPrice.GotFocus
  If txtPrice.ReadOnly Then
      ToolStripStatusLabel1.Text = ""
  Else
      ToolStripStatusLabel1.Text = "Price is not required"
  End If
End Sub

Private Sub cmbSelectPublisher_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSelectPublisher.GotFocus
  If cmbSelectPublisher.Enabled = False Then
      ToolStripStatusLabel1.Text = ""
  Else
  ToolStripStatusLabel1.Text = "Publisher is required"
  End If
End Sub

Private Sub edit()
    txtBookTitle.ReadOnly = False
    txtISBN.ReadOnly = False
    txtDescription.ReadOnly = False
    txtSubject.ReadOnly = False
    txtPages.ReadOnly = False
    txtPublicationYear.ReadOnly = False
    txtPrice.ReadOnly = False
    cmbSelectPublisher.Visible = True
    lblSelectPublisher.Visible = True

End Sub

Private Sub add()
    txtPublisher.Clear()
    txtAddress.Clear()
    txtCity.Clear()
    txtState.Clear()
    txtZipCode.Clear()
    txtPublicationYear.Text = ""
    txtBookTitle.ReadOnly = False
    txtISBN.ReadOnly = False
    txtDescription.ReadOnly = False
    txtSubject.ReadOnly = False
    txtPages.ReadOnly = False
    txtPublicationYear.ReadOnly = False
    txtPrice.ReadOnly = False
    btnForward.Enabled = False
    btnLast.Enabled = False
    btnAdd.Enabled = False
    btnBack.Enabled = False
    btnFirst.Enabled = False
    btnEdit.Enabled = False
    btnDelete.Enabled = False
    btnExit.Enabled = False
    btnSave.Enabled = True
    btnUndo.Enabled = True
    cmbSelectPublisher.Enabled = True
    cmbSelectPublisher.Visible = True
    lblSelectPublisher.Visible = True

End Sub

Private Sub undo()
    txtBookTitle.ReadOnly = True
    txtISBN.ReadOnly = True
    txtDescription.ReadOnly = True
    txtSubject.ReadOnly = True
    txtPages.ReadOnly = True
    txtPublicationYear.ReadOnly = True
    txtPrice.ReadOnly = True
    btnForward.Enabled = True
    btnLast.Enabled = True
    btnAdd.Enabled = True
    btnBack.Enabled = False
    btnFirst.Enabled = False
    btnEdit.Enabled = True
    btnDelete.Enabled = True
    btnExit.Enabled = True
    lblSelectPublisher.Visible = False
    cmbSelectPublisher.Visible = False
    btnSave.Enabled = False
    btnUndo.Enabled = False

End Sub
End Class

Recommended Answers

All 3 Replies

I am not seeing a handler for the form close event, is something firing off - or is it not being handled?

As for currency, try this:

txtPrice.Text = CDec(txtPrice.Text).ToString("C")

As for the navigation, are you scrolling, or clicking to the next entry?

When I exit, I get a nullreferenceexception that highlights "If drpub.IsNameNull Then" and it says "Object reference not set to an instance of an object."

Also, for my btnsave_click, how would I make it so when I save, it checks to make sure everything is entered correctly and then saves to the database and disables specific buttons?

You could create some functions that cast the data entered to the type.

Just check for failures, and then prompt the user with the problem.

Something like this:

Private Function CheckIfInteger(ByVal sVal As String)
    Try
        Cint(sVal)
        Return True
    Catch ex As Exception
        MsgBox(ex.Message & vbcrlf & "Please check data entered! '" & sVal & "' is invalid!")
        Return False
    End Try
End Sub

Just create one for each data type, and halt on error. (False value returned)

As for the NullReference. I am not sure where you get this declaration:

Dim drpub As _books_Fall2012_A2DataSet.PublishersRow

Do you have a class named "_books_Fall2012_A2DataSet"?

I think what you are wanting is Something like this:

'***DISCLAIMER***
'Can't test this code, so I can tell if it works or not.
Dim drpub As New String() {_books_Fall2012_A2DataSet.PublishersRow}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.