-1

When I run the vb proj this msg prompts.[ Visual Basic 2010]

Conversion from type 'DBNull' to type 'String' is not valid

Imports System.Data.OleDb

Public Class frmBooks
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data_ Source=G:\MINI PRIJECT ATI\LIBRARY SYSTEM.accdb;")

    Dim cmd As New OleDbCommand
    Dim selectedBookId As Integer
    Dim selectedClassId As String
    Dim selectedBookClass As String
    Dim selectedBookName As String
    Dim selectedISBN As String
    Dim selectedPublisher As String
    Dim selectedAuthor As String
    Dim selectedPublishYear As String
    Dim selectedEditn As Integer
    Dim selectedBookPrice As String
    Dim selectedAvailability As String
    Dim selectedShelfNo As String

    Private Sub ClearControls()
        txtBookId.Text =String.Empty
        txtClassId.Text = String.Empty
        cmbBookclass.Text = String.Empty
        txtBookname.Text = String.Empty
        txtISBN.Text = String.Empty
        txtPublisher.Text = String.Empty
        txtAuthor.Text = String.Empty
        txtPublishYear.Text = String.Empty
        txtEditn.Text = String.Empty
        txtBookprice.Text = String.Empty
        cmbAvailability.Text = String.Empty
        txtShelfNo.Text = String.Empty

    End Sub



    Private Sub frmBooks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'LIBRARY_SYSTEMDataSet.LIBRARY__BOOK_Query' table. You can move, or remove it, as needed.
        Me.LIBRARY__BOOK_QueryTableAdapter.Fill(Me.LIBRARY_SYSTEMDataSet.LIBRARY__BOOK_Query)
        LoadGrid(selectedBookId, selectedClassId, selectedBookClass, selectedBookName, selectedISBN, selectedPublisher, selectedAuthor, selectedPublishYear, selectedEditn, selectedBookPrice, selectedAvailability, selectedShelfNo)

    End Sub

    Private Sub SetControlValues()

        txtBookId.Text = selectedBookId
        txtClassId.Text = selectedClassId
        cmbBookclass.Text = selectedBookClass
        txtBookname.Text = selectedBookName
        txtISBN.Text = selectedISBN
        txtPublisher.Text = selectedPublisher
        txtAuthor.Text = selectedAuthor
        txtPublishYear.Text = selectedPublishYear
        txtEditn.Text = selectedEditn
        txtBookprice.Text = selectedBookPrice
        cmbAvailability.Text = selectedAvailability
        txtShelfNo.Text = selectedShelfNo

    End Sub



    Private Sub EnableControls(isEnable As Boolean)
        txtBookId.Enabled = isEnable
        txtClassId.Enabled = isEnable
        cmbBookclass.Enabled = isEnable
        txtBookname.Enabled = isEnable
        txtISBN.Enabled = isEnable
        txtPublisher.Enabled = isEnable
        txtAuthor.Enabled = isEnable
        txtPublishYear.Enabled = isEnable
        txtEditn.Enabled = isEnable
        txtBookprice.Enabled = isEnable
        cmbAvailability.Enabled = isEnable
        txtShelfNo.Enabled = isEnable
        btnSaved.Enabled = isEnable
        btnSearch.Enabled = isEnable


    End Sub



    Private Sub dgvBooks_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dgvBooks.SelectionChanged

        Try
            EnableControls(False)
            If dgvBooks.SelectedCells.Count > 0 Then
                Dim selectedRawIndex As Integer = dgvBooks.SelectedCells(0).RowIndex
                Dim selectedRow As DataGridViewRow = dgvBooks.Rows(selectedRawIndex)

                selectedBookId = selectedRow.Cells(colBookID.Name).Value
                selectedClassId = selectedRow.Cells(colClassId.Name).Value
                selectedBookClass = selectedRow.Cells(colBookClass.Name).Value
                selectedBookName = selectedRow.Cells(colBookName.Name).Value
                selectedISBN = selectedRow.Cells(colISBN.Name).Value
                selectedAuthor = selectedRow.Cells(colAuthor.Name).Value
                selectedPublishYear = selectedRow.Cells(colPublishYear.Name).Value
                selectedBookPrice = selectedRow.Cells(colBookPrice.Name).Value
                selectedAvailability = selectedRow.Cells(colAvailability.Name).Value
                selectedShelfNo = selectedRow.Cells(colShelfNo.Name).Value

                SetControlValues()

            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub


    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        ClearControls()
        LoadGrid()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadGrid(txtBookId.Text, txtClassId.Text, cmbBookclass.Text, txtBookname.Text, txtISBN.Text, txtPublisher.Text, txtAuthor.Text, txtPublishYear.Text, txtEditn.Text, txtBookprice.Text, cmbAvailability.Text, txtShelfNo.Text)

    End Sub


    Private Sub btnSaved_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaved.Click
        Dim ds As New DataSet
        Dim da As New OleDbDataAdapter
        Dim sqlStatement As String = ""
        Dim statusMsg As String = ""

        EnableControls(False)
        Try
            con.Open()
            cmd.Connection = con

            If selectedBookId = -1 Then
                sqlStatement = " INSERT INTO LIBRARY_BOOK (Book ID,Book Class Id,Book Class,Book Name,ISBN -10,Publisher,Author,Publishing Year,Edition,Price,Shelf Number,Availability3) VALUES('" + txtBookId.Text + "','" + txtClassId.Text + "','" + cmbBookclass.Text + "','" + txtBookname.Text + "','" + txtISBN.Text + "','" + txtPublisher.Text + "','" + txtAuthor.Text + "','" + txtPublishYear.Text + "','" + txtEditn.Text + "','" + txtBookprice.Text + "','" + txtShelfNo.Text + "','" + cmbAvailability.Text + "')"
                statusMsg = "A new book has added! "
            ElseIf selectedBookId > -1 Then
                sqlStatement = "UPDATE BOOK SET Book_ID='" + txtBookId.Text + "',Book Class_Id='" + txtClassId.Text + "',Book_Class='" + cmbBookclass.Text + "',Book_Name='" + txtBookname.Text + "',ISBN_-10='" + txtISBN.Text + "',Publisher='" + txtPublisher.Text + "',Author='" + txtAuthor.Text + "',Publishing_Year='" + txtPublishYear.Text + "',Edition='" + txtEditn.Text + "',Price='" + txtBookprice.Text + "',Shelf_Number='" + txtShelfNo.Text + "',Availability3='" + cmbAvailability.Text + "' WHERE = " + selectedBookId.ToString
                statusMsg = "BOOK " + selectedBookName + " has updated!"
            End If

            If Not String.IsNullOrEmpty(sqlStatement) Then
                cmd.CommandText = sqlStatement
                cmd.ExecuteNonQuery()
            End If



        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Close()

            LoadGrid()
            MsgBox(statusMsg)
        End Try
    End Sub






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

    Private Sub btnAddnw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddnw.Click
        EnableControls(True)
        ClearControls()
        txtBookId.Focus()
        selectedBookId = -1
    End Sub

    Private Sub LoadGrid(Optional ByVal bookId As String = "", Optional ByVal classId As String = "", Optional ByVal bookClass As String = "", Optional ByVal bookName As String = "", Optional ByVal isbn As String = "", Optional ByVal publisher As String = "", Optional ByVal author As String = "", Optional ByVal publishingYear As String = "", Optional ByVal edition As String = "", Optional ByVal bookPrice As String = "", Optional ByVal shelfNo As String = "", Optional ByVal availability As String = "")

        Dim ds As New DataSet
        Dim da As New OleDbDataAdapter
        Dim sqlStatement As String

        Try
            dgvBooks.AutoGenerateColumns = False
            EnableControls(False)
            con.Open()

            cmd.Connection = con
            sqlStatement = "SELECT * FROM LIBRARY_BOOK WHERE (Book ID LIKE '%" + bookId + "%') AND (Class ID LIKE '%" + classId + "%') AND (Book Class LIKE '%" + bookClass + "%') AND (Book Name LIKE '%" + bookName + "%') AND (ISBN LIKE '%" + isbn + "%') AND (Publisher LIKE '%" + publisher + "%')AND (Author LIKE '%" + author + "%') AND (Publish Year LIKE '%" + publishingYear + "%') AND (Edition LIKE '%" + edition + "%') AND (Price LIKE '%" + bookPrice + "%') AND (Shelf No LIKE '%" + shelfNo + "%') AND (Availability3 LIKE '%" + availability + "%')"
            cmd.CommandText = sqlStatement

            da = New OleDbDataAdapter(cmd)
            da.Fill(ds, "LIBRARY BOOK")

            If ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dgvBooks.DataSource = ds.Tables(0)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            con.Close()
        End Try




    End Sub


End Class

1 Answer 1

2

If it crashes on the load-event i'd add an exception-handler to the things that happen in the load-form. Then step through it with F10. You should be able to ID the value that causes the exception. You probably have to initalize some values so that it won't zero-ref when your query returns empty data. For instance you could say Dim bookname as string = "" and later you assign the data to it. Then the string is "" and not nothing.

Edit:

I'd recommend you coding with option strict on (add option strict on on top of your .vb-file I'm getting some warnings from your code, because you have not clearly defined some data-types.

To debug your code, change the load-function to this:

     Try
        Me.LIBRARY__BOOK_QueryTableAdapter.Fill(Me.LIBRARY_SYSTEMDataSet.LIBRARY__BOOK_Query)
        LoadGrid(selectedBookId, selectedClassId, selectedBookClass, selectedBookName, selectedISBN, selectedPublisher, selectedAuthor, selectedPublishYear, selectedEditn, selectedBookPrice, selectedAvailability, selectedShelfNo)

    Catch ex As Exception
        MsgBox("load exception  " & vbCrLf & ex.Message)
    End Try

then you mark your cursor in front of the "try" and press strg + F10. It will execute until it reaches the breakpoint. Then you can step through the code and see each procedure-step taken. So you can "F10" through all variable-assignments until the exception occurs.

However i cannot do much more, i do not have all the data you have. But i think you have a good base to go ahead, analyze the issue yourself and be successful in the end :)

1
  • Thank you fr the raply. As I new to VB can you show me how to do that. Please edit my codings.
    – CsJ
    Jul 10, 2015 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.