filter combobox in a datgrid based on another combobox vb.net

Hi ,,

i have problem with combobox

i have two combobox and two tables
table manages (ManageID, ManageName)
table Departments (DeptID, DeptName, Dept_ManageID)
1- CboEmp_Manage
2- CboEmp_Dept

Public Class Class1
Public sub FillCombobox (ByVal cbo As Combobox, ByVal TableName As String , ByVal Col_name As String , ByVal Col_Value As String )
Try

Dim VarSql As String
VarSql = ""
VarSql = "Select " + Col_name + " , " + Col_Value + " From " + TableName + " "
Dim sda As New SqlDataAdapter(VarSql , SQLCon)
Dim ds As New Dataset ()
sda.Fill(ds)
cbo.DataSource = ds.Tables(0)
cbo.DisplayMember = Col_name
cbo.ValueMember = Col_Value
Catch
End Try
End Sub

Private Sub CboEmp_Manage_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboEmp_Manage.SelectedValueChanged

WinClass.FillCombobox (CboEmp_Dept, "Departments WHERE Dept_Manage_ID='" & CboEmp_Manage.SelecteIndex.ToString() & "' " , "Dept_Name", "Dept_ID")

End Sub

its work but its give me wrong data from table

VarSql = "Select " + Col_name + " , " + Col_Value + " From " + TableName + " "

Avoid using this kind of code...it is prone to sql injection...

am trying to use this code but give wrong data

Dim udtsqlcmd as string
udtsqlcmd = ("select * From Departments Where Dept_manage_ID= '" & CboEmp_manage.selectedindex.tostring & "' ")

Dim dr As SQLDatadReader
Dim cmd As New SqlCommand
cmd = New SqlCommand(udtsqlcmd, Class1.sqlcon)
dr = cmd.ExecuteReader
CboEmp_Dept.Items.Clear()
Do While dr.Read
CboEmp_Dept.Items.Add(dr.Item("Dept_Name"))
Loop
dr.Close()
Class1.sqlcon.Close()

You are trying to put all the code together my friend..learned to sergrate the code of specific function that they are trying to do..

Dim udtsqlcmd as string
udtsqlcmd = ("select * From Departments Where Dept_manage_ID= '" & CboEmp_manage.selectedindex.tostring & "' ")

are you sure this is correct??..

ok how can i do that

i want to filter cboemp_dept based on cboemp_manage

learn me please

Try this logic

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ComboBox1.Items.Add(New DataItem(1, "Male"))
        ComboBox1.Items.Add(New DataItem(2, "Female"))
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim DI As DataItem
        Dim MySex As Integer

        DI = ComboBox1.SelectedItem
        MySex = CInt(DI.Data)

        MsgBox(MySex)

    End Sub

End Class

And i created class here is it

Public Class DataItem

Private ID As Object
Private MySex As String

Public Property Data() As Object
    Get
        Data = ID
    End Get
    Set(ByVal value As Object)
        ID = value

    End Set
End Property
Public Property Sex() As String
    Get
        Sex = MySex
    End Get
    Set(ByVal value As String)
        MySex = value
    End Set
End Property

Public Overrides Function ToString() As String
    ToString = MySex
End Function

Sub New(Optional ByVal Data As Object = Nothing, Optional ByVal Text As String = "")

    MySex = Text
    ID = Data

End Sub

End Class

Actually i also found this logic here in daniWeb..

This article has been dead for over six months. Start a new discussion instead.