No error in code but exception unhandled was appear during debugging

ismetothman

New member
Joined
Feb 22, 2021
Messages
1
Programming Experience
Beginner
Hello, i would like to ask for solution in my code after i got 'exception unhandled' at line 13 da.Fill(dt) . here is my codes

VB.NET:
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.DataTable
Public Class frmMain
    Dim source1 As New BindingSource()
    Dim source2 As New BindingSource()
    Dim ds As DataSet = New DataSet
    Dim tables As DataTableCollection = ds.Tables
    Sub fill()
        Dim dt As New DataTable
        Dim str As String = "Select NoAhli as [Nom Ahli], NamaPenuh as [Nama Penuh], NoKP as [Nom KP], Jawatan as [Jawatan], TarafKeahlian as [Taraf Keahlian] from perinfo ORDER By NamaPenuh "
        Dim da As New OleDb.OleDbDataAdapter(str, con)
        da.Fill(dt)
        da.Dispose()
        source1.DataSource = dt
        DataGridView1.DataSource = dt
        DataGridView1.Refresh()
        DataGridView1.Columns(4).Width = 150
    End Sub
    Public Sub clear()
        NoAhli.Clear()
        NamaPenuh.Clear()
        NoKP.Clear()
        Jawatan.SelectedIndex = -1
        TarafKeahlian.SelectedIndex = -1
    End Sub
    Public Sub Clearitems()

        ListView1.Items.Clear()
    End Sub

    Public Sub delterminated(ByRef id As String)
        Try
            Dim str1 As String = "Delete * from perinfo where [perinfo.NoAhli]= '" & id & "'"
            Dim cmd1 As New OleDbCommand(str1, con)
            con.Open()
            cmd1.ExecuteNonQuery()
            con.Close()
            fill()
        Catch ex As Exception
            ex.ToString()
        End Try
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fill()
    End Sub

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
        'If TextBox1.Text = DataGridView1.SelectedCells(0).Value.ToString Then

        ' End If
        Dim sql As String = "Select * FROM perinfo where NoAhli = '" & NoAhli.Text & "' "
        Dim cmd1 As New OleDbCommand(sql, con)
        con.Open()
        Using reader As OleDbDataReader = cmd1.ExecuteReader()
            If reader.HasRows Then
                MsgBox("Nombor ahli telah wujud!")
                NoAhli.Focus()
                NoAhli.SelectAll()
            Else
                ListView1.Items.Add(NoAhli.Text.ToString)
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Format(NamaPenuh.Text).ToString)
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Format(NoKP.Text).ToString)
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Format(Jawatan.Text).ToString)
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Format(TarafKeahlian.Text).ToString)
                clear()
            End If
            con.Close()
        End Using
    End Sub

    Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click
        Try
            For Each item As ListViewItem In ListView1.Items
                If con.State = ConnectionState.Closed Then con.Open()
                Dim st As String = "INSERT INTO [perinfo](NoAhli, NamaPenuh, NoKP, Jawatan, TarafKeahlian) VALUES ('@NoAhli','@NamaPenuh', '@NoKP', '@Jawatan', '@TarafKeahlian' )"
                Dim cmdd As New OleDbCommand(st, con)
                cmdd.ExecuteNonQuery()
                con.Close()
                Me.fill()
                Clearitems()
            Next
            MessageBox.Show("Proses Telah Berjaya!", "Simpan", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoAhli.TextChanged

    End Sub

    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
        frmUpdate.TextBox1.Text = Me.DataGridView1.SelectedCells(0).Value.ToString
        frmUpdate.TextBox2.Text = Me.DataGridView1.SelectedCells(1).Value.ToString
        frmUpdate.TextBox3.Text = Me.DataGridView1.SelectedCells(3).Value.ToString
        frmUpdate.TextBox4.Text = Me.DataGridView1.SelectedCells(2).Value.ToString
        frmUpdate.TextBox5.Text = Me.DataGridView1.SelectedCells(4).Value.ToString
        frmUpdate.Show()
    End Sub

    Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label10.Click
        If DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 0 Then
            If DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 1 Then
                Dim dr As New DialogResult
                dr = MessageBox.Show("Anda pasti untuk padam data ini selamanya?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
                If dr = Windows.Forms.DialogResult.Cancel Then
                    MessageBox.Show("Permintaan dibatalkan!", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else
                    For i = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                        delterminated(DataGridView1.SelectedRows(i).Cells(0).Value)
                        fill()
                    Next
                End If
            Else

                Dim dr As New DialogResult
                dr = MessageBox.Show("Adakah anda pasti untuk padam selamanya? " & DataGridView1.SelectedCells(0).Value & "?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
                If dr = Windows.Forms.DialogResult.Cancel Then
                    MessageBox.Show("Action aborted!", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    DataGridView1.ClearSelection()
                    Exit Sub
                Else
                    delterminated(DataGridView1.SelectedCells(0).Value)
                    fill()
                End If
            End If
        End If

    End Sub
 
Just like when you posted this at SO, you need to provide the error message so that we know what we're looking for.

Also, please don't just post all your code when the vast majority of it is irrelevant. The more irrelevant code you post, the harder it is for us to identify the relevant code and the less likely you'll get the help you want.
 
If I had to guess, which I do, I'd say that the issue is with your connection object. I don't see where you've declared con in any of that code - missing relevant code amongst all the irrelevant - or set its connection string so I suspect that the connection string is either not set or is incorrect. Of course, the error message would give us a pretty good indication of whether that's the case or not.
 
Back
Top