Outside Computers

Monday, March 22, 2010

Adding items in combo boxes from mysql database in vb.net

In this post, I will give some sample codes on how to populate your combo box from your database using datasets.

In this example code, I will use MYSQL as my database and VB.net 2005 teamsuite.

First part of the code: we write a code on setting up a connection on our database and here it is.
Dim dataset1 As DataSet = New DataSet
Dim dataadapter1 As MySqlDataAdapter = New MySqlDataAdapter

Dataset this is where we will put all the data that we will get from our datasources
Dataadapter this is our connection or bridge to our connection object and our dataset


2nd part : Getting the data from the database

Dim msql As String = "Select EmpId from tblempinfo" 'SQL query to select what data will be shown in the combo box
myda1.SelectCommand = New MySqlCommand(msql, mysqlcon) 'will trigger the selected sql query to get the data we needed


3rd Part: filling up the Combo box

myda1.Fill(myds1) 'the data adapter will fill our dataset to hold the data
cboEmp.DataSource = myds1.Tables(0) 'this code will connect the combo box and the database using datasets ,setting up a data source.
cboEmp.DisplayMember = "EmpId" 'this will filter what data to be displayed on the combo box
cboEmp.ValueMember = "EmpId" 'this will set or get the actual value of the displayed member.



I hope this sample code will help you

Enjoy Coding.



On the next post : setting up the reports from the selected value on the combo box

No comments:

Post a Comment