[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I populate a Visual Studio 2003 dropdownlist

Posted on 2009-02-17
6
Medium Priority
?
297 Views
Last Modified: 2012-05-06
How do I populate the dropdownlist using
dataTestfield and datavaluefield?
I have included code that does noit populate the list
Dim sqlConnection As New SqlConnection(CStr(Application("TestConnString")))
 
        Dim strSQL As String = "procGetSales"
        Dim sqlCommand As New SqlCommand
 
        Try
 
            Try
                sqlConnection.Open()
            Catch exc As Exception
                lblMessages.Text = exc.Message()
            End Try
            sqlCommand.CommandText = strSQL
            sqlCommand.Connection = sqlConnection
            sqlCommand.CommandType = CommandType.StoredProcedure
            sqlCommand.Parameters.Add("@nvchrAction", "GET SALES")
 
            Dim rdrMyQuery As SqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
 
            'Dim temo As 
            cboSales.DataSource = rdrMyQuery
            cboSales.Items.Add("Select from List")
            Do While rdrMyQuery.Read
                'cboSales.Items.Add(rdrMyQuery.GetString(0))
                cboSales.DataTextField = rdrMyQuery.GetString(0)
                cboSales.DataValueField = CStr(rdrMyQuery.GetInt32(1))
            Loop
            cboSales.DataBind()
            rdrMyQuery.Close()
        Catch
            lblMessages.Text = "<font color='RED'>" + Err.Description + "</font>"
        End Try
        sqlCommand.Dispose()
        sqlConnection.Close()
        sqlConnection.Dispose()

Open in new window

0
Comment
Question by:expertsexchangehunter
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 23660996
Hello expertsexchangehunter,

You are mixing and matching your methods here.

You need to either do:

            Dim rdrMyQuery As SqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
 
            'Dim temo As
            cboSales.DataSource = rdrMyQuery
            cboSales.DataTextField = "The FieldName In The Reader That Should be displayed"
            cboSales.DataValueField = "The Fieldname in the reader that should be used as the value property for the item, can be the same as the text field above"
            cboSales.Items.Insert(0, New ListItem("Select from List","Select"))
            cboSales.DataBind()
            rdrMyQuery.Close()

Or

            Do While rdrMyQuery.Read
                cboSales.Items.Add (New ListItem(rdrMyQuery.GetString(0),CStr(rdrMyQuery.GetInt32(1)))
            Loop

Regards,

TimCottee
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 23661005
expertsexchangehunter,

In the second instance you do not need the .DataBind() because you are explicitly adding the items to the collection, in the first case you do need to bind because you are binding the specified fields to the text and value properties of the items collection.

TimCottee
0
 

Author Comment

by:expertsexchangehunter
ID: 23661596
I used the first solution with the datatestfield and datavaluefield and the list is properly populated but when I go to add the selected datavaluefield it contains the name I used for
            cboSales.DataValueField = "The Fieldname in the reader that should be used as the value property for the item, can be the same as the text field above"

when it should be the value from the database table for the specific record. What am I doing wrong?
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 43

Accepted Solution

by:
TimCottee earned 2000 total points
ID: 23662186
You set them as appropriate, if you have for example ID and Description as columns in your resultset then set

.DataValueField = "ID"
.DataTextField = "Description"
0
 

Author Comment

by:expertsexchangehunter
ID: 23662243
I have
              cboSales.DataSource = rdrMyQuery
            cboSales.DataTextField = "SalesName"
            cboSales.DataValueField = "intSalesPersonID"
            cboSales.Items.Insert(0, New ListItem("Select from List", "Select"))
            cboSales.DataBind()
            rdrMyQuery.Close()
0
 

Author Comment

by:expertsexchangehunter
ID: 23663217
I changed the select statement to use ID for the datavalue field
Also the select uses as to set the values
select dsds and sdsds as salesnem and dfdfds as ID
chould this make a difference?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

868 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