0
 Private Sub btnSelectAsset_Click(sender As Object, e As EventArgs) Handles btnSelectAsset.Click
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString =
    "server=localhost;userid=root;password= ;database=myma"
    Dim SDA As MySqlDataAdapter
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource

    Try
        MySqlConn.Open()
        COMMAND.Connection = MySqlConn
        SDA = New MySqlDataAdapter("select * from myma.user", MySqlConn)
        dbDataSet = New DataTable
        SDA.Fill(dbDataSet)
        DataGridView1.DataSource = dbDataSet
        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()
    End Try
End Sub

object reference not set to an instance of an object.

I got the above error when debugging. Could you please help me correct this.

11
  • Which line you getting error? Commented Nov 6, 2015 at 5:34
  • There is no line specified Commented Nov 6, 2015 at 5:35
  • Please debug and check which line you getting error. Commented Nov 6, 2015 at 5:36
  • Warning 1 There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "aspnet_regsql", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. MY MA Commented Nov 6, 2015 at 5:40
  • 1
    From the code there, the COMMAND object (which isnt used) must not be instantiated. Get rid of the Try/Catch and it will tell you which object on which line you didnt initialize. Get more info from the questions on the right under Related or hundreds of other questions here on the NullReferenceException Commented Nov 6, 2015 at 6:07

1 Answer 1

0

Try this working example:

Private Sub btnSelectAsset_Click(sender As Object, e As EventArgs) Handles btnSelectAsset.Click
    Dim dbConn = New MySqlConnection
    Dim dbDataTable = New DataTable

    dbConn.ConnectionString = "server=localhost;userid=root;password= ;database=myma"

    Try
        dbConn.Open()
        If (dbConn.State = ConnectionState.Open) Then
            Dim dbAdapter = New MySqlDataAdapter("select * from myma.user", dbConn)
            dbAdapter.Fill(dbDataTable)
            DataGridView1.DataSource = dbDataTable
            dbConn.Close()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        dbConn.Dispose()
    End Try
End Sub

VB2010Ex & .NET 4 Client Profile.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.