943,670 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 10172
  • VB.NET RSS
Jul 22nd, 2008
0

filling data in combo box from SQL server

Expand Post »
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.

vb.net Syntax (Toggle Plain Text)
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3. Dim conn As New System.Data.SqlClient.SqlConnection("Network Library=DBMSSOCN;" & "Data Source=abacus\mydatabase;" & "Initial Catalog=CADCAM2;" & "Integrated Security =True;" & "MultipleActiveResultSets=True;")
  4. Dim strSQL As String = "SELECT * FROM product"
  5. Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, conn)
  6. Dim ds As New DataSet
  7. da.Fill(ds, "product")
  8.  
  9. With Me.cmbDropDown
  10. .DataSource = ds.Tables("product")
  11. .DisplayMember = "product_name"
  12. .ValueMember = "product_id"
  13. .SelectedIndex = 0
  14. End With
  15.  
  16. end sub

vb.net Syntax (Toggle Plain Text)
  1. Private Sub cmbCproduct_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCproduct.SelectedValueChanged
  2.  
  3. 'when the user selects the product_name from the combo box i should be able to get the product_id.
  4.  
  5. End Sub
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
buffdaemon_live is offline Offline
3 posts
since Jul 2008
Jul 22nd, 2008
-1

Re: filling data in combo box from SQL server

Use another SQL statement. Construct it like this-

vb.net Syntax (Toggle Plain Text)
  1. Dim strSQL2 As String = "SELECT product_id FROM product where product_name=" & Me.cmbDropDown.SelectedItem
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jul 22nd, 2008
0

Re: filling data in combo box from SQL server

The problem with this query is that i can have some of the "product_name" same. Any idea??

Click to Expand / Collapse  Quote originally posted by tuse ...
Use another SQL statement. Construct it like this-

vb.net Syntax (Toggle Plain Text)
  1. Dim strSQL2 As String = "SELECT product_id FROM product where product_name=" & Me.cmbDropDown.SelectedItem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
buffdaemon_live is offline Offline
3 posts
since Jul 2008
Jul 23rd, 2008
0

Re: filling data in combo box from SQL server

you must have other key to differentiating the same product_name.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Aug 20th, 2011
0
Re: filling data in combo box from SQL server
hi all
i want when i select data from my combobox the data apprear in my textbox1
from my sql database server there are two column in my table itemname,itemcode
itemname is already in my combobox
i want itemcode appear in textbox1 when i select itemname from combobox
thank you
and this is my code

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.Data.SqlClient
  2. Public Class Received_Items
  3. Dim con As SqlConnection
  4. Dim cmd As SqlCommand
  5. Dim dr As SqlDataReader
  6. Dim strsql As String, i As Integer = 0
  7.  
  8.  
  9. Private Sub Received_Items_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10. con = New SqlConnection("Data Source=win7-pc\sqlexpress;Initial Catalog=catering;Integrated Security=True;Pooling=False")
  11. con.Open()
  12. cmd = New SqlCommand(strsql, con)
  13. strsql = "select distinct itemname from bstore"
  14. cmd = New SqlCommand(strsql, con)
  15.  
  16. dr = cmd.ExecuteReader
  17.  
  18. selectComboBox1.Items.Clear()
  19. Do While dr.Read
  20. selectComboBox1.Items.Add(dr("itemname"))
  21. Loop
  22. dr.Close()
  23.  
  24.  
  25. End Sub
  26.  
  27. Private Sub selectComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles selectComboBox1.TextChanged
  28. If Txtitmcode.Text = "" Then
  29. Exit Sub
  30. End If
  31. Dim strsql As String, i As Integer = 0
  32. con = New SqlConnection("Data Source=win7-pc\sqlexpress;Initial Catalog=catering;Integrated Security=True;Pooling=False")
  33. con.Open()
  34.  
  35. strsql = "select * from bstore where itemcode=" & selectComboBox1.SelectedItem
  36.  
  37.  
  38. cmd = New SqlCommand(strsql, con)
  39.  
  40. dr = cmd.ExecuteReader
  41.  
  42. If Not dr.HasRows Then
  43. MsgBox("not find")
  44. Else
  45.  
  46. dr.Read()
  47. Txtitmcode.Text = dr("itemcode")
  48.  
  49. End If
  50.  
  51.  
  52. dr.Close()
  53.  
  54. End Sub
  55. End Class
Last edited by Narue; Aug 22nd, 2011 at 2:44 pm. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Airheart is offline Offline
1 posts
since Aug 2011
Dec 8th, 2011
0
Re: filling data in combo box from SQL server
buff see below

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub cmbCproduct_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCproduct.SelectedValueChanged
  2. 'when the user selects the product_name from the combo box i should be able to get the product_id.
  3. Dim productID As Integer
  4. productID = cmbCproduct.SelectedValue
  5.  
  6. msgbox(productID)
  7. End Sub
Reputation Points: 11
Solved Threads: 14
Posting Whiz in Training
bluehangook629 is offline Offline
204 posts
since Dec 2009
Dec 8th, 2011
0
Re: filling data in combo box from SQL server
for you airheart,
set your item name displayed member and item code as valued member.
At .selectedvaluechanged have textbox1 = .selectedvalue
but before you need to populate your combobox and set your combobox properties described above.
see buffdaemon's code for populating combobox.

If you need further help start a new thread
Last edited by bluehangook629; Dec 8th, 2011 at 12:56 pm.
Reputation Points: 11
Solved Threads: 14
Posting Whiz in Training
bluehangook629 is offline Offline
204 posts
since Dec 2009
Message:
Previous Thread in VB.NET Forum Timeline: Moving 2 Listbox
Next Thread in VB.NET Forum Timeline: select in vb.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC