0

Currently I want to show data from SQL into DataGridView on VB.Net. But Add.Rows return Object reference not set to an instance of an object Error on my Project. So my original Code looks like this.

Imports System.Data.Odbc
Public DataTabel As DataTable

Assign Tabel for DataGridView

Sub Tabel()
        DataTabel = New DataTable

        With DataTabel.Columns
            .Add("kodebarang")
            .Add("namabarang")
            .Add("satuan")
            .Add("harga", GetType(Double))
            .Add("jumlah", GetType(Integer))
            .Add("hargatotal", GetType(Double))
        End With

        DataGridView1.DataSource = DataTabel

        With DataGridView1
            .Columns(0).HeaderText = "Kode Barang"
            .Columns(1).HeaderText = "Nama Barang"
            .Columns(2).HeaderText = "Satuan"
            .Columns(3).HeaderText = "Harga"
            .Columns(4).HeaderText = "Qty"
            .Columns(5).HeaderText = "Harga Total"

            .Columns(3).DefaultCellStyle.Format = "###,###,###"
            .Columns(5).DefaultCellStyle.Format = "###,###,###"

            .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            .Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

            .Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

            .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End With
    End Sub

Execute Add Rows to DataGridView from SQL

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Call Connect()

        Sql = "SELECT * FROM transaksi_partai where faktur_partai='" & TextBox1.Text & "'"
        Cmd = New OdbcCommand(Sql, Con)
        Read = Cmd.ExecuteReader

        If Read.HasRows = True Then
            ComboBox1.Text = Read("customer")
            Label4.Text = Read("nilai_nota").ToString
            DateTimePicker1.Value = Read("tanggal_penjualan")

            Call Connect()

            Sql = "SELECT * FROM transaksi_partai_detail WHERE faktur_nota='" & TextBox1.Text & "'"
            Cmd = New OdbcCommand(Sql, Con)
            Read = Cmd.ExecuteReader

            While Read.Read()
                DataTabel.Rows.Add(Read("kodebarang"), Read("namabarang"), Read("satuan"), Val(Read("harga")), Val(Read("jumlah")), Val(Read("hargatotal")))
                'Object reference not set to an instance of an object
            End While

            TextBox6.Enabled = True
            Button3.Enabled = True
            Button4.Enabled = True
            Button5.Enabled = True
            Button6.Enabled = True
            Button7.Enabled = False
            Button8.Enabled = True
            Button9.Enabled = True
            Label12.Text = DataGridView1.Rows.Count
        ElseIf Read.HasRows = False Then
            Exit Sub
        End If
    End Sub

I change my code to this and its work fine without error. except if i enable the Exception

If Read.HasRows = True Then
            Try
                ComboBox1.Text = Read("customer")
                Label4.Text = Read("nilai_nota").ToString
                DateTimePicker1.Value = Read("tanggal_penjualan")

                Call Connect()

                Sql = "SELECT * FROM transaksi_partai_detail WHERE faktur_nota='" & TextBox1.Text & "'"
                Cmd = New OdbcCommand(Sql, Con)
                Read = Cmd.ExecuteReader

                While Read.Read()
                    DataTabel.Rows.Add(Read("kodebarang"), Read("namabarang"), Read("satuan"), Val(Read("harga")), Val(Read("jumlah")), Val(Read("hargatotal")))
                End While

                TextBox6.Enabled = True
                Button3.Enabled = True
                Button4.Enabled = True
                Button5.Enabled = True
                Button6.Enabled = True
                Button7.Enabled = False
                Button8.Enabled = True
                Button9.Enabled = True
                Label12.Text = DataGridView1.Rows.Count
            Catch ex As Exception
                'MsgBox("DataGridView Tidak Terupdate!", MsgBoxStyle.Exclamation, "Error!")
            End Try
        ElseIf Read.HasRows = False Then
            Exit Sub
        End If
GaroPpo
  • 3
  • 8
  • Are you sure there is not a DBnull value that cause this error and not . DataTabel.NewRow? Also before in your code was Dim DataTabel = New DataTable inside sub Tabel, that means it was a local variable and DataTabel outside global variable was Nothing?? – G3nt_M3caj Jul 27 '20 at 15:10
  • yes, there is not DBnull Value cause this error. For `Dim DataTabel = New DataTable` inside sub Tabel, it;s my mistake, I tried change to it before but i return to original one `DataTabel = New DataTable` – GaroPpo Jul 27 '20 at 15:34
  • I already edit my question where I add `Try Catch ex As Exception` but somehow it works without error. Still confused what cause my error though. – GaroPpo Jul 27 '20 at 15:35

0 Answers0