Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Visual Basic (VB 4/5/6) » Database Programming » filling data in combo box from SQL server


filling data in combo box from SQL serverExpand / Collapse
Author
Message
Posted 7/22/2008 10:50:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 9/4/2008 4:11:31 PM
Posts: 3, Visits: 3
Dear all,

I wanted to fill the combo box with the data from my SQL server database.
There are two column in the table, product_id and product_name.

for example:

product_id| product_name
---------- --------------
1| pencil
2| NoteBook

I was successful in displaying the product name in combo box. The code is given below. I wanted to show the product_name in the combo box and when the user selects the product_name from combo box i should be able to get its product_id. How do i do it? Please help me guys for my academic project.

Below is my code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim conn As New System.Data.SqlClient.SqlConnection("Network Library=DBMSSOCN;" & "Data Source=abacus\mydatabase;" & "Initial Catalog=CADCAM2;" & "Integrated Security =True;" & "MultipleActiveResultSets=True;")
Dim strSQL As String = "SELECT * FROM product"
Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "product")

With Me.cmbDropDown
.DataSource = ds.Tables("product")
.DisplayMember = "product_name"
.ValueMember = "product_id"
.SelectedIndex = 0
End With

end sub


Private Sub cmbCproduct_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCproduct.SelectedValueChanged

'when the user selects the product_name from the combo box i should be able to get the product_id.
End Sub
Post #25445
Posted 9/19/2008 10:43:13 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: 10/14/2008 12:47:36 PM
Posts: 17, Visits: 20
Use the SelectedIndexChanged event
Post #25737
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Brian, Peter

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 12:17am