none
how to update combobox value without closing the form RRS feed

  • Question

  • pls help me, my combobox has a value from mysql database and i have a child form to add data in my database, what i want to do is after adding a data in my database my combobox will also update its value

    my code in populating my combo box

        Private Sub patientinfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           Dim READER As MySqlDataReader
                con.Open()
                Dim Query As String
                Query = "SELECT DISTINCT province FROM address"
                command = New MySqlCommand(Query, con)
                READER = command.ExecuteReader
                While READER.Read
                    Dim sName = READER.GetString("province")
                    cbpro.Items.Add(sName)
                End While
                con.Dispose()
                
        End Sub

    Thursday, November 29, 2018 12:24 PM

Answers

  • pls help me, my combobox has a value from mysql database and i have a child form to add data in my database, what i want to do is after adding a data in my database my combobox will also update its value

    my code in populating my combo box

        Private Sub patientinfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           Dim READER As MySqlDataReader
                con.Open()
                Dim Query As String
                Query = "SELECT DISTINCT province FROM address"
                command = New MySqlCommand(Query, con)
                READER = command.ExecuteReader
                While READER.Read
                    Dim sName = READER.GetString("province")
                    cbpro.Items.Add(sName)
                End While
                con.Dispose()
                
        End Sub

    Hi no one,

    I think that this code may help you ....... I'm using it to perform the same mission

    Sub fillCategory() Try Dim CN As New OleDbConnection(cs) CN.Open() Dim da As New OleDbDataAdapter() da.SelectCommand = New OleDbCommand("SELECT distinct (CategoryName) FROM InventoryCategory", CN) Dim ds As New DataSet("ds") Dim dt As New DataTable

    da.Fill(ds) dt = ds.Tables(0) cmbCategory.Items.Clear() ComboBox1.Items.Clear()

    For Each drow As DataRow In dt.Rows cmbCategory.Items.Add(drow(0).ToString()) ComboBox1.Items.Add(drow(0).ToString()) Next CN.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub


    And then put it in the Form_Load or in any event you want (Like SelectedIndexChanged of another combobox ) or (In validating event) this is for example ...........

    Hope it helps 


    Regards From Amr_Aly



    • Edited by Amr_Aly Friday, November 30, 2018 7:45 PM
    • Marked as answer by no[one] Saturday, February 23, 2019 10:27 AM
    Friday, November 30, 2018 7:33 PM

All replies

  • You need to add a button to do reload.
    Thursday, November 29, 2018 1:10 PM
  • If your child form is displayed using ShowDialog, then execute the code (which currently is in patientinfo_Load but can be moved to a separate function) immediately after ShowDialog.

    Otherwise give some details about the usage of your forms.

    • Edited by Viorel_MVP Thursday, November 29, 2018 1:18 PM
    Thursday, November 29, 2018 1:16 PM
  • You would get the value via SelectedIndexChanged event of the ComboBox and use it (as mentioned to add) with a SQL INSERT statement using a parameter for each value being inserted.

    Private Sub cbpro_SelectedIndexChanged(sender As Object, e As EventArgs) _
        Handles cbpro.SelectedIndexChanged
    
        Dim province = cbpro.Text
    
    End Sub

    Simple example of adding a parameter were cmd is a command object

    cmd.Parameters.Add("?province", MySqlDbType.VarChar).Value = province


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 29, 2018 1:23 PM
    Moderator
  • pls help me, my combobox has a value from mysql database and i have a child form to add data in my database, what i want to do is after adding a data in my database my combobox will also update its value

    my code in populating my combo box

        Private Sub patientinfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           Dim READER As MySqlDataReader
                con.Open()
                Dim Query As String
                Query = "SELECT DISTINCT province FROM address"
                command = New MySqlCommand(Query, con)
                READER = command.ExecuteReader
                While READER.Read
                    Dim sName = READER.GetString("province")
                    cbpro.Items.Add(sName)
                End While
                con.Dispose()
                
        End Sub

    Hi no one,

    I think that this code may help you ....... I'm using it to perform the same mission

    Sub fillCategory() Try Dim CN As New OleDbConnection(cs) CN.Open() Dim da As New OleDbDataAdapter() da.SelectCommand = New OleDbCommand("SELECT distinct (CategoryName) FROM InventoryCategory", CN) Dim ds As New DataSet("ds") Dim dt As New DataTable

    da.Fill(ds) dt = ds.Tables(0) cmbCategory.Items.Clear() ComboBox1.Items.Clear()

    For Each drow As DataRow In dt.Rows cmbCategory.Items.Add(drow(0).ToString()) ComboBox1.Items.Add(drow(0).ToString()) Next CN.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub


    And then put it in the Form_Load or in any event you want (Like SelectedIndexChanged of another combobox ) or (In validating event) this is for example ...........

    Hope it helps 


    Regards From Amr_Aly



    • Edited by Amr_Aly Friday, November 30, 2018 7:45 PM
    • Marked as answer by no[one] Saturday, February 23, 2019 10:27 AM
    Friday, November 30, 2018 7:33 PM