-2

I'm Using DataGridView to Display my MySQL Table.

Here's my code:

Public Class Form2
    Dim sqlQuery as String
    Dim sqlQuery As String
    Dim sqlCon As MySqlConnection
    Dim sqlCmd As MySqlCommand
    Dim sqlData As MySqlDataReader
    Dim sqlAdapter As MySqlDataAdapter

    Dim bindEmp As BindingSource
    Dim tblEmp As DataTable

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles       MyBase.Load
          Call SQLConn()

          sqlQuery = "SELECT * FROM employee_info"
          sqlCmd = New MySqlCommand(sqlQuery,sqlCon)
          sqlAdapter.SelectCommand = sqlCmd
          sqlAdapter.Fill(tblEmp)
          bindEmp.DataSource = tblEmp
          DGVStaffAccounts.DataSource = bindEmp
          sqlAdapter.Update(tblEmp)
          End Sub

End Class

I want to display the result, but I'm having a "NullReferenceException" at sqlCmd = New MySqlCommand(sqlQuery, sqlCon)

I don't understand why sqlCmd is Null. Any help would be appreciated.

*EDIT

this is the code inside SQLConn()

Private Sub SQLConn()
    sqlCon = New MySqlConnection("host=localhost; user=root; password=; database=rfid")
    Try
        sqlCon.Open()
    Catch ex As Exception
        MessageBox.Show("Cannot Connect to SQL. Exiting Program")
        Me.Close()
    End Try
End Sub

I'm sorry if I didn't include the code inside SQLConn()

Skeeed Row
  • 27
  • 4

1 Answers1

0

sqlCon variable is not initialized. Initialize it before passing it to sqlCmd.