473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to populate combobox in VB.net

11 New Member
Hi, I've re-framed my question. Could anyone solve this for me.

I'm using VB 2010 and Access Database file. The database consists of a table called 'usertbl' with 'username', 'type' and 'level' fields. The records are like follows:

Record 1 - Username: admin Type: admin Level:1
Record 2 - Username: user1 Type: user Level:2
Record 1 - Username: user2 Type: user Level:1

User's Combobox (cboUser) which populated with the username field and the cboType with the Type field. What I want is when the selecting item from the cbouser, the cboType combobox shows only the user's type such as when selecting 'admin' from cboUser, the cboType shows only admin and when select 'user' from cboUser, cboType should shows 'user1' and 'user2'. When both combo boxes are filled it populated a datagridview. But for all works only the problem is when I select 'admin' from cboUser, in the cboType it shows all three users (admin, user1, user2); where only admin should have been displayed.

Here is my code...

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.OleDb
  2.  
  3. Public Class Form1
  4.  
  5. Sub fillcombo()
  6.  
  7. mysql = "Select distinct * From Usertbl"
  8.  
  9. Dim cboCmd As New OleDb.OleDbCommand
  10. cboCmd.CommandText = mysql
  11. cboCmd.Connection = dataconn
  12. dataread = cboCmd.ExecuteReader
  13. While (dataread.Read())
  14. cboName.Items.Add(dataread("username"))
  15.  
  16. End While
  17. cboCmd.Dispose()
  18. dataread.Close()
  19.  
  20. End Sub
  21.  
  22. Sub fillcbotype()
  23.  
  24. mysql = "Select distinct [Type] From Usertbl"
  25.  
  26. Dim cboCmd As New OleDb.OleDbCommand
  27.  
  28. cboCmd.CommandText = mysql
  29. cboCmd.Connection = dataconn
  30. dataread = cboCmd.ExecuteReader
  31. While (dataread.Read())
  32. cbotype.Items.Add(dataread("Type"))
  33.  
  34. End While
  35. cboCmd.Dispose()
  36. dataread.Close()
  37. End Sub
  38.  
  39. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  40.  
  41. Module1.Connect()
  42. Me.fillcombo()
  43. Me.fillcbotype()
  44. Me.myselection()
  45.  
  46. End Sub
  47.  
  48. Private Sub datagridshow()
  49. Dim ds As New DataSet
  50. Dim dt As New DataTable
  51.  
  52. ds.Tables.Add(dt)
  53. Dim da As New OleDbDataAdapter
  54.  
  55. da = New OleDbDataAdapter("Select * From Usertbl", dataconn)
  56. da.Fill(dt)
  57.  
  58. DataGridView1.DataSource = dt.DefaultView
  59. dataconn.Close()
  60.  
  61.  
  62. End Sub
  63.  
  64. Private Sub cboName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboName.SelectedIndexChanged
  65. cbochange()
  66.  
  67.  
  68.  
  69. End Sub
  70. Private Sub cbochange()
  71. Dim ds As New DataSet
  72. Dim dt As New DataTable
  73.  
  74. ds.Tables.Add(dt)
  75. Dim da As New OleDbDataAdapter
  76.  
  77. da = New OleDbDataAdapter("Select Distinct * From Usertbl where username= '" & cboName.Text & "' and type='" & cbotype.Text & "'", dataconn)
  78. da.Fill(dt)
  79.  
  80.  
  81. DataGridView1.DataSource = dt.DefaultView
  82. dataconn.Close()
  83. End Sub
  84.  
  85. Private Sub cbotype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbotype.SelectedIndexChanged
  86.  
  87. myselection()
  88.  
  89. End Sub
  90.  
  91. Private Sub myselection()
  92.  
  93. Dim mycon As New OleDb.OleDbConnection
  94. mycon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= 'C:\Users\Genius\Desktop\VBCode\Usertable.mdb'"
  95. mycon.Open()
  96.  
  97.  
  98. 'dataconn.Open()
  99. mysql = "Select * From Usertbl where username = '" & cbotype.Text & "' and type= '" & cbotype.Text & "'"
  100. Dim mycmd As New OleDbCommand
  101. mycmd.CommandText = mysql
  102. mycmd.Connection = mycon
  103. dataread = mycmd.ExecuteReader
  104. If (dataread.Read() = True) Then
  105. cboName.Text = (dataread("Username"))
  106. cbotype.Text = (dataread("type"))
  107. End If
  108.  
  109. mycmd.Dispose()
  110. dataread.Close()
  111. mycon.Dispose()
  112.  
  113. End Sub
  114. End Class
Jan 7 '13 #1
1 2715
Rabbit
12,516 Recognized Expert Moderator MVP
Is this the same issue as your other thread? If it is, I will merge your two threads because we only allow one thread per question to avoid confusion and doubling up on efforts.
Jan 7 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jassim Rahma | last post by:
Hi, what is the best way to populate comboBox from database.. the reason I am asking is that i have more than 8 comboBoxes in one form and each should retrieve data from a table and I don't want...
0
by: ROO | last post by:
Hi Everyone, I have a database table that have 4 field( C1, C2, M1, M2) on my form i have two combo box ComboC and ComboM C1 C2 M1 M2 1 ...
1
by: Primera | last post by:
I'm trying to populate a ComboBox on Form.load with a list of the OU's pulled from AD. Here is the code that does not work: Dim rootdir As New System.DirectoryServices.DirectoryEntry _...
11
by: DSR | last post by:
Help Please... I would like to populate a combo box on a form with a query that compares data from two tables. Any record that is unique in table1 should continue to populate my combobox. The...
16
by: agrawal.solutions | last post by:
Hello Friends I am asking a very silly question but i dont find any solution fo this.. I am selectiong a recordset and want to populate a combobox where id would be inviseble and the content...
0
by: vnaz235 | last post by:
Hi, guys, 1. Can anybody help me to populate ComboBox on VB6 form with a) text strings from the range on the already existing Excel sheet (e.g.,...
1
by: freekedoutfish | last post by:
Hi. New member here Im sat at work, pounding my head off the desk because this tiny bit of simple code refuses to work. The sub is intended to pull data from the "companyname" column in the...
1
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, I've a column in a dataGridView set as combobox. I can't find any example showing how to populate the list in the combobox. Does anyone have an example showing me this. Regards Jesper.
4
by: gobblegob | last post by:
hi guys, I would like to populate a combobox list with the contents of a folder which contains text files , which will later be opened. Thanks in advanced for any help :) Gobble.
1
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
This actually stemmed from anther post I created. But, I have two combo boxes. ComboBox1 is populated by a list of Systems (DataSet from Oracle Query). Now, I want to take Combobox2, and when...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.