Solved

How to bind one field of a DataSet to a DropDownList

Posted on 2008-10-07
8
274 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now