It was exception unhandled during debugging my vb project

Ismet Othman 1 Reputation point
2021-02-22T19:28:16.04+00:00

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

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Duane Arnold 3,221 Reputation points
    2021-02-23T05:39:21.467+00:00

    How are we suppose to know what line 13 is? Why can't you just post the single line of code that through the exception? Why do we need to see all the other code that has nothing to do with the exception?

    How are we to know what exception you got if you don't post the exception message?

    0 comments No comments

  2. Karen Payne MVP 35,401 Reputation points
    2021-02-23T14:11:20.207+00:00

    Hello,

    In the future always place code into a code block via the 5th button from the left in the editor toolbar.

    In regards to the error, we need to know what the exact error message is. Suggest using one of the following, report back here what the error message is.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
        Handles MyBase.Load 
    
        Try
            fill() 
        Catch ex As Exception 
            MessageBox.Show(ex.Message)
        End Try 
    
    End Sub 
    

    Or

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
        Handles MyBase.Load 
    
        Try
            fill() 
        Catch ex As Exception 
            Debug.WriteLine(ex.Message)
        End Try 
    
    End Sub 
    

    Other recommendations

    • Never use column names in your database with spaces e.g. use NomAhli rather than Nom Ahli. Spaces can be done in various ways in code
    • Don't load data in form load, use form Shown event instead
    • Write each query in the database rather than code and then copy-n-paste into code
    • in the Fill procedure get rid of da.Dispose(), it's not needed.
    • If the code worked at one point then use your version control to revert back to the point.
    0 comments No comments

  3. Peter Fleischer (former MVP) 19,321 Reputation points
    2021-02-24T06:36:54.75+00:00

    Hi Ismet,
    at first Set Option Strict On. Use Try / Catch to catch error message. Show error message. Change code like this:

    Option Strict On
    
    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()
        Try
          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
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
      End Sub
    ...
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.