Solved

How to bind one field of a DataSet to a DropDownList

Posted on 2008-10-07
8
276 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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