473,749 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ 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 2737
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
3254
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 that to slow the form show. can you please submit a sample code...
0
2138
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 Sales 1 Monthly Terms 1 Sales 2 Annual Terms
1
2212
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 _ ("LDAP://dc=corp,dc=abc,dc=com") Dim rootsearch As New System.DirectoryServices.DirectorySearcher(rootdir) Dim result As System.DirectoryServices.SearchResult rootsearch.SearchScope = DirectoryServices.SearchScope.Subtree rootsearch.Filter =...
11
12309
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 instant the record appears also in table2 it should no longer be listed in my combo box. This is not a key field in either of the tables. Basically what I am trying to do is prevent the user from entering duplicate values even though it is not a key...
16
10530
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 would. I am getting the recordset and the no of record but then i am unble to populate the combobox. I have already tried all the function starting form for each x in .... and while...wend and do....loop
0
4006
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., Workbook("Test1").Sheets("Sheet1").Range("P12:P72")) and another ComboBox with b) names of visible Excel sheets in the same workbook? 2. How to pass selected items from ComboBox1 into Workbook("Test1").Sheets("Sheet1").Range("A1") and from ComboBox2 into...
1
6001
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 "cmSelectAllCompanyNames" table, and put all the company names it finds into the "cbFilterCompanyName" combo box. Now the confusing aspect is this: It works!!!
1
12714
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
5712
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
2296
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 the data is changed on Combox1, I want to populate data depending on the selection in Combox1. So, ComboBox1 lists "OracleDB123". I want ComboBox2 to list all accounts on that system. That information is stored in say the Accounts
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9388
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.