Solved

Combobox filled using datatable fails

Posted on 2008-10-10
2
720 Views
Last Modified: 2012-05-05
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
Comment
Question by:logoncom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 22694267
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
 

Author Closing Comment

by:logoncom
ID: 31505024
Thanks... that helped
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question