Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 730
  • Last Modified:

Combobox filled using datatable fails

I am trying to get a list of items to fill in a combobox.

I call a function and expect a return list.... what is wrong with my code?  
cbbae.DataSource = employeelist("sales")
 
 
 
    Public Shared Function employeelist(ByVal Dept As String)
 
        Dim cbb As ComboBox = New ComboBox
        Dim dt As DataTable = New DataTable
        Using conn As New SqlConnection(myconnectionstring)
            Using cmd As New SqlCommand("APP_GetEmployeeList", conn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@department", Dept)
 
 
                Using da As New SqlDataAdapter(cmd)
 
 
                    da.Fill(dt)
                    With cbb
                        .DataSource = dt
                        .DisplayMember = "repcode"
                    End With
 
 
                End Using
            End Using
        End Using
 
        Return cbb.DisplayMember
 
    End Function

Open in new window

0
logoncom
Asked:
logoncom
1 Solution
 
CodeCruiserCommented:
1)The first problem i can see is that there is no return type defined for the function. Functions are defined as
public shared function employeelist(dept as string) as string()
end function

2) you are calling the function agaist the datasource property but setting the datasource within the function and return the displaymember which is a string

Try with the following code.
cbbae.DataSource = employeelist("sales")
cbbae.DisplayMember = "repcode"
 
Public Shared Function employeelist(ByVal Dept As String) as DataTable
        Dim dt As DataTable = New DataTable
        Using conn As New SqlConnection(myconnectionstring)
            Using cmd As New SqlCommand("APP_GetEmployeeList", conn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@department", Dept)
                Using da As New SqlDataAdapter(cmd)
                     da.Fill(dt)
                 End Using
            End Using
        End Using
         Return dt
     End Function

Open in new window

0
 
logoncomAuthor Commented:
Thanks... that helped
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now