Solved

How to bind one field of a DataSet to a DropDownList

Posted on 2008-10-07
8
270 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
Comment Utility
<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
Comment Utility
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
Comment Utility
How are you data-binding the DropDownList?
0
 

Author Comment

by:nightshadz
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:nightshadz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

11 Experts available now in Live!

Get 1:1 Help Now