RSS Forums RSS

filling data in combo box from SQL server

Please support our VB.NET advertiser: DiscountASP.NET – 3 Months Free on VB.NET Web Hosting
Reply
Posts: 3
Reputation: buffdaemon_live is an unknown quantity at this point 
Solved Threads: 0
buffdaemon_live buffdaemon_live is offline Offline
Newbie Poster

filling data in combo box from SQL server

  #1  
Jul 22nd, 2008
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.

  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

  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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 158
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 10
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: filling data in combo box from SQL server

  #2  
Jul 22nd, 2008
Use another SQL statement. Construct it like this-

  1. Dim strSQL2 As String = "SELECT product_id FROM product where product_name=" & Me.cmbDropDown.SelectedItem
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote  
Posts: 3
Reputation: buffdaemon_live is an unknown quantity at this point 
Solved Threads: 0
buffdaemon_live buffdaemon_live is offline Offline
Newbie Poster

Re: filling data in combo box from SQL server

  #3  
Jul 22nd, 2008
The problem with this query is that i can have some of the "product_name" same. Any idea??

Originally Posted by tuse View Post
Use another SQL statement. Construct it like this-

  1. Dim strSQL2 As String = "SELECT product_id FROM product where product_name=" & Me.cmbDropDown.SelectedItem
Reply With Quote  
Posts: 2,628
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 244
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: filling data in combo box from SQL server

  #4  
Jul 23rd, 2008
you must have other key to differentiating the same product_name.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the VB.NET Forum
Views: 2067 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:37 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC