Click here to Skip to main content
15,742,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get my combo box to refresh and have new values(name) automatically whenever the user adds new data to the database from a separate form (LoanForm).

Goal: The ComboxBox should reflect new names after adding new data from LoanForm. Screenshot:

Screenshot LoanForm:
(LoanForm adding data works perfectly)

What I have tried:

I wrote a code that makes the comboBox reflect values after adding from another form, so when I navigate to "collections form" and search their name it should reflect: Screenshot:

The code for ComboBox:
 Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star"
    Dim da As New MySqlDataAdapter
    Dim dt As New DataTable
    Dim query As String
    query = "select * from loan"
    Command = New MySqlCommand(query, MysqlConn)
    da.SelectCommand = Command
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "account_name"
End Sub

But the code to refresh new values doesn't work until I run the program which I don't want that.

I have another code that allows users to bind data from comboBox to textbox according to name selection but if I do input the above code for refresh values the binding of data to textbox doesn't work. Two of them don't work unless I remove one of them. Can anyone assist me with what went wrong?
Thank you.
Updated 30-Jan-23 8:14am
Member 15627495 28-Jan-23 18:10pm    
define your DT as a global public var , you can reach it through forms.
the way you invoke the DT you're limited in scope level.
put it as 'header var' in your class.
Member 15627495 28-Jan-23 18:16pm    
all the resources you use, are too isolated ( stuck ) in sub .
prevent yourself from redundant code with the use of a module 'db access and connection', and with locating your vars at a 'level with benefits' , public vars are not more heavy than the way you do, but they will be write just one time, instead of writing them in each sub or functions you write.

Its 'software with code design and structure'.
Member 15627495 28-Jan-23 18:21pm    
public DT as datatable // can be useful through all your software.
private sub do-stuff()
dim DT as datable // for 'local' use only, restricted for the sub .
end sub


Google Search is a great research tool.

Here is a search based on your requirements: winform combobox db refresh - Google Search[^]

... and it turned up this great answer as the first result: How to refresh data driven combo box on a winform - StackOverflow[^]

Here is another search: bind combobox to database c# refresh[^]

... and it turned up another great answer as the first result: Refresh ComboBox Items, easiest way - StackOverflow[^]

Yes, both answers are in C#. Here's the rub, there is more content for C# than VB, so you have a better chance of finding answers for C# than VB. The code in both of those links is simple, and should not present any issues converting to VB.

To demonstrate, try this search: bind combobox to database vb refresh - Google Search[^]

.., and turned up yet another great answer (C#): Refresh combo box in Windows Form VB.NET[^]
Share this answer
I'd strongly recommend to read these excellent articles:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

Then you'll understand how to transfer data between forms.

When you use a database, you have to call method which will refresh a second form.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900