| | |
filling data in combo box from SQL server
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
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)
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
vb.net Syntax (Toggle Plain Text)
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
Use another SQL statement. Construct it like this-
vb.net Syntax (Toggle Plain Text)
Dim strSQL2 As String = "SELECT product_id FROM product where product_name=" & Me.cmbDropDown.SelectedItem
My blog on .NET- http://dotnet.tekyt.info
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
The problem with this query is that i can have some of the "product_name" same. Any idea??
•
•
•
•
Use another SQL statement. Construct it like this-
vb.net Syntax (Toggle Plain Text)
Dim strSQL2 As String = "SELECT product_id FROM product where product_name=" & Me.cmbDropDown.SelectedItem
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 *
So, Please do something before post your thread.
* PM Asking will be ignored *
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
Other Threads in the VB.NET Forum
- Previous Thread: video captions in vb.net
- Next Thread: Tools in vb.net windows application
| Thread Tools | Search this Thread |
.net .net2005 .net2008 30minutes 2005 2008 access account arithmetic array basic browser button buttons center check code component connectionstring crystalreport cuesent data database databasesearch datagrid datagridview date datetimepicker design dissertation dissertations dissertationthesis dissertationtopic dropdownlist excel fade file-dialog filter folder ftp generatetags hardcopy image images input insert intel monitor navigate net networking opacity output panel passingparameters peertopeervideostreaming picturebox picturebox1 port printing problem problemwithinstallation project reports" savedialog searchvb.net select settings shutdown string survey tcp temperature text textbox timer timespan toolbox transparency trim updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year






