Solved

Combobox filled using datatable fails

Posted on 2008-10-10
2
722 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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