Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jmcilhinney

Thread: im trying to fill my combo box with my database.. HELP.

  1. #1
    nhonoskywalker is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    5
    Reputation
    0

    im trying to fill my combo box with my database.. HELP.

    I searched google and youtube for tutorials they made with their codes but mine NOT..
    Please tell me hot to do it with my code or tell me what is wrong..
    it is the Imports MySql.Data.MySqlClient ? Because I didnt saw this code from their tutorials.


    Imports MySql.Data.MySqlClient

    Public class main

    Private Sub cboemprecDept_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboemprecDept.Click
    cboDept_load()
    End Sub



    Private Sub cboDept_load()
    Dim strsql As String
    Dim sqlCommand As New MySqlCommand


    If sConnection.State = ConnectionState.Closed Then
    sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = 1111111; DATABASE = ftenn;"
    sConnection.Open()
    End If
    Dim reader As MySqlDataReader = sqlCommand.ExecuteReader
    strsql = "SELECT * FROM dept_tab"
    With sqlCommand
    .CommandText = strsql
    .Connection = sConnection
    .ExecuteNonQuery()
    End With



    While reader.Read
    With cboemprecDept.Items.Add(reader("dept_id"))
    End With
    End While

    sConnection.Close()
    End Sub
    End Class

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,341
    Reputation
    1543
    First things first, you don't call ExecuteNonQuery to retrieve data. "non-query" means "not a SELECT statement". You ARE executing a query. Call ExecuteReader to create a data reader. Create a DataTable and call its Load method to load the data from the data reader into the DataTable. You can then bind that DataTable to the ComboBox.
    Using connection As New MySqlConnection("connection string here"),
    command As New MySqlCommand("SQL query here", connection)
    connection.Open()

    Using reader = command.ExecuteReader()
    Dim table As New DataTable

    table.Load(reader)

    With myComboBox
    .DisplayMember = "Name"
    .ValueMember = "ID"
    .DataSource = table
    End With
    End Using
    End Using
    nhonoskywalker likes this.

  3. #3
    Sparrow is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2012
    Posts
    6
    Reputation
    0
    Module SqlFunctions

    Public Function filldatatable(ByVal sql As String) As DataTable
    Cursor.Current = Cursors.WaitCursor
    'Mysql Dimms
    Dim cs As String
    cs = "Your Connection String"
    Dim con As New MySqlConnection(cs)
    Dim ds As New DataTable
    Dim da As New MySqlDataAdapter




    Try
    Try


    con.Open()
    da = New MySqlDataAdapter(sql, con)
    da.Fill(ds)
    filldatatable = ds
    con.Close()
    Cursor.Current = Cursors.Default
    Return filldatatable
    Catch ex As Exception
    MessageBox.Show("Unable to communicate with sql server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    MsgBox(ex.ToString)
    End Try
    Catch myerror As MySqlException
    MessageBox.Show("Error Updating Databse : " & myerror.Message)


    End Try
    Cursor.Current = Cursors.Default


    End Function
    End Module

    Then in your form you add a data set then you fill the data se as so:

    Dim Sql As String


    Sql = "your query"
    Dim nametbl As DataTable = sqlFunctions.filldatatable(Sql)
    For Each row As DataRow In nametbl.Rows()
    Dim newrow As DataRow
    newrow = DataSet.Tables().NewRow
    newrow.Item(0) = row.Item(0)
    DataSet.Tables().Rows.Add(newrow)
    Next
    cbServers.DataSource = nametbl
    cbServers.ValueMember = "name"
    cbServers.SelectedIndex = 0

    cbServers is the combobox
    Last edited by jmcilhinney; 07-09-2012 at 11:56 PM. Reason: Added [xcode=vb] tags for readability

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking