By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,065 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,065 IT Pros & Developers. It's quick & easy.

How to populate combobox in VB.net

P: 11
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
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,383
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

Post your reply

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