Search code examples
vb.netconnectionsqldataadapter

The DataAdapter doesn't open the connection itself with Fill Method


I'm using next sub to fill combobox by using datatable through dataadapter:

Public Sub Me_Sub_CboFill(ByVal Cbo As ComboBox, ByVal SqlStr As String, ByVal Dm As String, ByVal Vm As String)
    Cbo.DataSource = Nothing
    xAdapter = New MySqlDataAdapter(SqlStr, Conn)
    Dim Dt As New DataTable
    xAdapter.Fill(Dt)

    If Dt.Rows.Count = 0 Then Conn.Close()

    Cbo.DataSource = Dt
    Cbo.DisplayMember = Dm
    Cbo.ValueMember = Vm

End Sub

but I face next MSG:

unable to connect to any of the specified MySQL hosts but when I open the connection manually, it works!!

I know that dataadapter with (Fill) opens and closes the connection itself but I didn't know why that happened with my code.

by the way, I tried many ways to test that but with the same result, like next code:

    dim dt as new datatable
    Dim xx As New MySqlDataAdapter(SqlNat, Conn)
    MsgBox(Conn.State)
    xx.Fill(Dt)

thanks


Solution

  • I found the problem with my code I put the connection string inside function that I use to check the connection and for that I needed to call the function at least one time to make everything work correctly.

    thanks all for all your information best,