Solved

How to bind one field of a DataSet to a DropDownList

Posted on 2008-10-07
8
279 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
MS Dynamics Made Instantly Simpler

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

 

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

Technology Partners: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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