Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two combobox that i want to load data based on the selected item on the first combobox.The code i have works but it displays the result like an array of characters showing one letter in each line instead of a word in each line.

Please help out. The code below is displaying the values in the combobox as an array of one letter in each line instead of a whole word in each line.
This is the code i have used now
Public Class Form1
    Dim TextingContext As New TestingEntities
    Dim currentDepartment As New Department

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles comFaculty.SelectedIndexChanged
        Dim selectedfaculty As Faculty = CType(comFaculty.SelectedItem, Faculty)
        Dim selectedFacultyID As Integer = selectedfaculty.Faculty_ID
        Dim dep As IQueryable(Of Department) = From p In TextingContext.Departments
                                             Where p.Faculty.Faculty_ID = selectedFacultyID
                                             Select p

        Dim selDepartment As List(Of Department) = dep.ToList
        currentDepartment = selDepartment.FirstOrDefault

        comDepartment.DisplayMember = "Department_Name"
        comDepartment.DataSource = currentDepartment.Department_Name.ToList
    End Sub
Updated 29-May-13 18:00pm

This is the code i used and it is working fine

If lstFaculty.SelectedItem Is Nothing Then Exit Sub

Dim selectedFaculty As Faculty = DirectCast(lstFaculty.SelectedItem, Faculty)
Dim selectedFacultyID As String = (selectedFaculty.Faculty_ID)


Dim depQuery As IQueryable(Of Department) = From dep In examinationOfficer.Departments
Where dep.Faculty_ID = selectedFacultyID
Select dep

lstDepartment.DataSource = depQuery.ToList
lstDepartment.DisplayMember = "Department_Name"

Thanks all for your time and effort
Share this answer
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New SqlDataAdapter("select * from student where id>3", db.con)
Dim ds As New DataSet
da.Fill(ds, "1")
For i As Integer = 0 To ds.Tables("1").Rows.Count - 1
Catch ex As Exception
MsgBox("Error : " + ex.Message)
End Try
End Sub

Kishor Makwana
Software Engineer
Insight Softech
Share this answer
Your error is in this line:

comDepartment.DataSource = currentDepartment.Department_Name.ToList

Presumably, Department_Name is a string. Converting a string to a list will yield a list of characters that comprise the string.

I would guess that you intended:

comDepartment.DataSource = selDepartment
Share this answer
Should'nt the select part be Select p.Department.Department_Name?

In general, debugging and stepping through your code will help you get an idea on the error.
Code will break on the line you have an error.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900