0

I get an exception when trying to link my form (created with VB.NET) to MySQL. When I try to run the code I get the following error message, System.NullReferenceException: 'Object reference not set to an instance of an object. sqlData was Nothing.'

    Dim sqlconn As New MySqlConnection
    Dim sqlcomm As New MySqlCommand
    Dim sqlrd As MySqlDataReader
    Dim sqlData As DataTable
    Dim sqlad As MySqlDataAdapter
    Dim sqlQuery As String

    Dim server As String = "localhost"
    Dim username As String = "root"
    Dim password As String = "ramdom password"
    Dim database_name As String = "trial"

    Private Sub UpdateTable()
        sqlconn.ConnectionString = "server =" + server + ";" + "username =" + username + ";" _
            + "password =" + password + ";" + "database =" + database_name
        sqlconn.Open()
        sqlcomm.Connection = sqlconn
        sqlcomm.CommandText = "SELECT * FROM trial.student"
        sqlrd = sqlcomm.ExecuteReader()
        sqlData.Load(sqlrd)
        sqlrd.Close()
        sqlconn.Close()
        DataGridView1.DataSource = sqlData
    End Sub

Upon researching the issue, it seems like the error is a result of the object not being initialized, although I don't know how to solve such a problem.

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24

0 Answers0