Solved

How to bind one field of a DataSet to a DropDownList

Posted on 2008-10-07
8
280 Views
Last Modified: 2010-04-14
I have a class that calls a stored procedure and returns a DataSet.  The DataSet has three columns.  How do I bind all the values of one particular field from the DataSet to a DropDownList?  I have the DropDownList coded on my ASP Page and I call the class from the VB page, but I'm not sure how to do this.

This is what I have, but it throws an error:
ASP:
<asp:DropDownList ID="ddlEntity" runat="server" DataValueField='<%# Bind("ENTITYID") %>'></asp:DropDownList>

VB:
        Dim myBL As New LIDBusinessLogic(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("connectionstring_2").ConnectionString)
        Me.ddlEntity.DataSource = myBL.LOCXLATDS()
        Me.ddlEntity.DataValueField = "ENTITYID"
        Me.ddlEntity.DataBind()
0
Comment
Question by:nightshadz
[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
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22663689
<asp:DropDownList ID="ddlEntity" runat="server" SelectedValue='<%# Bind("ENTITYID") %>'></asp:DropDownList>

 Dim myBL As New LIDBusinessLogic(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("connectionstring_2").ConnectionString)
        Me.ddlEntity.DataSource = myBL.LOCXLATDS()
        Me.ddlEntity.DataTextField = "ENTITYNAME"
        Me.ddlEntity.DataValueField = "ENTITYID"
        Me.ddlEntity.DataBind()

0
 

Author Comment

by:nightshadz
ID: 22668792
Thanks for the quick response!

The program still crashes on the DataBind method.  The three fields the DataSet returns are:
LOCATION1 VARCHAR(100)
LOCATIONID INTEGER
ENTITYID VARCHAR(15)

I figured the problem is with Me.ddlEntity.DataTextField = "ENTITYNAME.  I tried changing it to ENTITYID, commenting it out, but neither worked.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22675011
How are you data-binding the DropDownList?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:nightshadz
ID: 22678023
Via the code in my original post.  There's a class that calls a stored procedure which returns a DataSet of the three columns I listed above.
0
 

Author Comment

by:nightshadz
ID: 22678234
Just to add on to what I wrote above, I updated the code in my original post to what you had given me.

Here is the code which returns the DataSet I mentioned above:
        Try
            Dim myDA As New SqlDataAdapter
            Dim myDS As New DataSet
            Me.cmd.Connection = Me.cn
            Me.cmd.CommandText = Me.mySQL
            Me.cmd.CommandType = CommandType.StoredProcedure
            Me.cmd.CommandTimeout = 30
            myDA.SelectCommand = Me.cmd
            Me.cn.Open()
            myDA.Fill(myDS)
            Return myDS
        Catch ex As Exception
            Throw
        Finally
            Me.cn.Close()
        End Try
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22680071
1) What does 'mySQL' equal?

2) What columns to you get in the DataTable?

3) That could be simplified:


Dim dt As New DataTable()
Using adapter As New SqlDataAdapter(procedureName, connectionString)
   adapter.SelectCommand.CommandType = CommandType.StoredProcedure
   adapter.Fill(dt)
End Using

Open in new window

0
 

Author Comment

by:nightshadz
ID: 22680374
It's just running a stored procedure and returning a DataSet with these fields:

LOCATION1 VARCHAR(100)
LOCATIONID INTEGER
ENTITYID VARCHAR(15)

ENTITYID is what I want populated in the DropDownList.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 22681298
Then, you shouldn't be having those problems.  Because you are, it is my assumption that you are not getting the correct columns, or you are not binding to the DropDownList correctly.  Another thing to check would be that you are using the correct AccessDataSource.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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