0

So I'm having a bit of a pickle with my code in VB here.

I have a sub I would like to run whenever my Windows form load, so I create a separate sub then call in into a load event

Public Class Transfer

Dim ConnectionString As String = "my connection"

Dim rs As SqlDataReader
Dim dt As DataTable
Dim dieDT As DataTable

 Private Sub MaintEntry_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

    loadtable()

End Sub

Public Sub loadtable() 'load sql data into table

    Dim myCmd As String = "my SQL statement"

    'preemptively clear Datagridview of all data before new data loads
    dieDT.Clear() '!Here is the problem area!
    DieTable.DataSource = dieDT
    DieTable.DataSource = Nothing

    'call connection (myConn)
    Using myConn As SqlConnection = New SqlConnection(ConnectionString)
        myConn.Open()
        Using comm As SqlCommand = New SqlCommand(myCmd, myConn)

            Dim rs As SqlDataReader = comm.ExecuteReader
            dieDT.Load(rs)

            For i = 0 To dt.Rows.Count - 1
                Me.DieTable.Rows.Add()
                Me.DieTable.Item("Proj", i).Value = dieDT.Rows(i).Item("Project No")
                Me.DieTable.Item("Inv", i).Value = dieDT.Rows(i).Item("Inv No")
                Me.DieTable.Item("InvDesc", i).Value = dieDT.Rows(i).Item("Description")
                Me.DieTable.Item("E_Date", i).Value = dieDT.Rows(i).Item("Entry Date")
                Me.DieTable.Item("S_Date", i).Value = dieDT.Rows(i).Item("Date")
                Me.DieTable.Item("S_Time", i).Value = dieDT.Rows(i).Item("Time")
                Me.DieTable.Item("Details", i).Value = dieDT.Rows(i).Item("Problem + Repair Details")
                Me.DieTable.Item("status", i).Value = dieDT.Rows(i).Item("Status")
                Me.DieTable.Item("AccuStk", i).Value = dieDT.Rows(i).Item("Accumulative Stroke")
                Me.DieTable.Item("prevenStk", i).Value = dieDT.Rows(i).Item("Preventive Stroke")
                Me.DieTable.Item("Die_PIC", i).Value = dieDT.Rows(i).Item("PIC")
            Next
            DieTable.AutoGenerateColumns = False

        End Using
    End Using

    'make table read only
    DieTable.ReadOnly = True

End Sub
End Class

When I tested the code, I get a Object reference not set to an instance of an object. error on the dieDT.Clear() line. Even though I already declared dieDT at the start of code.

What could be the problem here?

hjh93
  • 570
  • 11
  • 27
  • The instance of the object was never created. – Pawel Apr 14 '20 at 03:59
  • This: `dieDT.Clear()` you don't need it. This: `DieTable.DataSource = dieDT DieTable.DataSource = Nothing` is obviously wrong and also not needed (if you did this the other way around, maybe...). Then, why do you load a DataTable, then try to add records to a DataGridView, using an `Item` property that it doesn't have, instead of simply set the DataSource to the `dieDT` DataTable you just (tried to) load? – Jimi Apr 14 '20 at 04:08
  • You need to spend more time on the basics. Declaring a variable and creating an object are two different things. If you could not detect a declared variable in your method then your code wouldn't compile. A run-time exception is something else entirely. – jmcilhinney Apr 14 '20 at 04:13

0 Answers0