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
- Imports System.Data.OleDb
- Public Class Form1
- Sub fillcombo()
- mysql = "Select distinct * From Usertbl"
- Dim cboCmd As New OleDb.OleDbCommand
- cboCmd.CommandText = mysql
- cboCmd.Connection = dataconn
- dataread = cboCmd.ExecuteReader
- While (dataread.Read())
- cboName.Items.Add(dataread("username"))
- End While
- cboCmd.Dispose()
- dataread.Close()
- End Sub
- Sub fillcbotype()
- mysql = "Select distinct [Type] From Usertbl"
- Dim cboCmd As New OleDb.OleDbCommand
- cboCmd.CommandText = mysql
- cboCmd.Connection = dataconn
- dataread = cboCmd.ExecuteReader
- While (dataread.Read())
- cbotype.Items.Add(dataread("Type"))
- End While
- cboCmd.Dispose()
- dataread.Close()
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Module1.Connect()
- Me.fillcombo()
- Me.fillcbotype()
- Me.myselection()
- End Sub
- Private Sub datagridshow()
- Dim ds As New DataSet
- Dim dt As New DataTable
- ds.Tables.Add(dt)
- Dim da As New OleDbDataAdapter
- da = New OleDbDataAdapter("Select * From Usertbl", dataconn)
- da.Fill(dt)
- DataGridView1.DataSource = dt.DefaultView
- dataconn.Close()
- End Sub
- Private Sub cboName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboName.SelectedIndexChanged
- cbochange()
- End Sub
- Private Sub cbochange()
- Dim ds As New DataSet
- Dim dt As New DataTable
- ds.Tables.Add(dt)
- Dim da As New OleDbDataAdapter
- da = New OleDbDataAdapter("Select Distinct * From Usertbl where username= '" & cboName.Text & "' and type='" & cbotype.Text & "'", dataconn)
- da.Fill(dt)
- DataGridView1.DataSource = dt.DefaultView
- dataconn.Close()
- End Sub
- Private Sub cbotype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbotype.SelectedIndexChanged
- myselection()
- End Sub
- Private Sub myselection()
- Dim mycon As New OleDb.OleDbConnection
- mycon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= 'C:\Users\Genius\Desktop\VBCode\Usertable.mdb'"
- mycon.Open()
- 'dataconn.Open()
- mysql = "Select * From Usertbl where username = '" & cbotype.Text & "' and type= '" & cbotype.Text & "'"
- Dim mycmd As New OleDbCommand
- mycmd.CommandText = mysql
- mycmd.Connection = mycon
- dataread = mycmd.ExecuteReader
- If (dataread.Read() = True) Then
- cboName.Text = (dataread("Username"))
- cbotype.Text = (dataread("type"))
- End If
- mycmd.Dispose()
- dataread.Close()
- mycon.Dispose()
- End Sub
- End Class