0

When I got the value for the combo box from the MySQL database it seems getting error declaration expected in line 4. Can anyone help me? Here I attach my code

    Imports MySql.Data.MySqlClient
    Public Class Utama
     Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click
        Me.Hide()
        Dim utama As New Login
        utama.Show()
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub SupplierToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SupplierToolStripMenuItem.Click
        Me.Hide()
        Dim supplier As New Supplier
        supplier.Show()
    End Sub

    Private Sub ProdukToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProdukToolStripMenuItem.Click
        Me.Hide()
        Dim Produk As New Produk
        Produk.Show()
    End Sub

    Private Sub CetakToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CetakToolStripMenuItem.Click
        Me.Hide()
        Dim Cetak As New Cetak
        Cetak.Show()
    End Sub

    Dim connection As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
    Dim da As New MySqlDataAdapter("select * from supplier", connection)
    Dim dt As DataTable
    da.fill(dt)
    ComboBox1.Datasource=dt
    ComboBox1.DisplayMember = "npwp" 
    ComboBox1.ValueMember = "npwp"  
End Class
  • 1
    This is not related to your issue but, when binding data to controls, you should pretty much ALWAYS set the `DataSource` last. That's because that's when the binding actually occurs so, if you do it first, the binding gets configured once than and then again when you set the other properties. Set those properties first so the binding is only configured once. – jmcilhinney Jul 08 '21 at 05:38

2 Answers2

1

You need to put that code inside a method. You can't just put arbitrary code anywhere in a class. The only thing that can be directly in the class is a declaration. The first three lines are declarations, so they're OK. The last four lines are not. That code should be inside a method somewhere. If you want to execute that code when the form loads then it should be in the Load event handler. This is pretty elementary stuff that any beginners tutorial would cover, so maybe you should work your way through such a tutorial.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • {Private Sub Utama_Load(sender As Object, e As EventArgs) Handles MyBase.Load da.Fill(dt) ComboBox1.DataSource = dt ComboBox1.DisplayMember = "npwp" ComboBox1.ValueMember = "npwp" End Sub } I have already use method but it getting error in the value cannot be null – Masa Depan Jul 08 '21 at 05:23
  • @MasaDepan, don't put information in comments on answers that belongs in the question. If it didn't work then you did it wrong. Edit your question to show the code you are currently using and indicate what the error is and exactly where it occurs. Also, you better have debugged your code BEFORE asking for help, i.e. if you're told that a value cannot be null then you better have actually looked to see what value is null when it happened. You have the debugger right in front of you. Use it. – jmcilhinney Jul 08 '21 at 05:37
  • @MasaDepan, for the record, if you do use the debugger then it should be obvious what the issue is and how to fix it. It has nothing to do with the original problem so does not form part of this question. For more information, read [this](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). – jmcilhinney Jul 08 '21 at 05:40
0

First, create a method as shown below and check the exception.

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

      Try

         Dim connection As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
         Dim da As New MySqlDataAdapter("select * from supplier", connection)
         Dim dt As new DataTable
         da.fill(dt)
         ComboBox1.Datasource = dt
         ComboBox1.DisplayMember = "npwp"
         ComboBox1.ValueMember = "npwp"

      Catch ex As Exception
         MsgBox(ex.ToString)
      End Try

   End Sub
Think2826
  • 201
  • 1
  • 7