Question : Referring to Data in a Drop Down List that is selected

I ahve the following code where I am opening an SQL Connection and executing a Sql Query to load and populate a Drop Down List Box which I have defined as DDLIssue.

My coding:

PopulateIssue()
    End Sub

    Sub PopulateIssue()

        'Create the connection
        'Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString
        Dim myConnection = New SqlConnection(clsDAL.CallIssueConnection())

        'Query to execute
        Dim strQuery As String = "Select IssueID, Issue FROM Issue ORDER BY Issue"

        'Create the Command
        Dim myCommand As New SqlCommand(strQuery, myConnection)

        'The Try Catch Statement determines whether there is a Database Connection
        'An error message appears informing the user.
        Try
            'Open the database connection
            myConnection.Open()

        Catch ex As Exception
            QueryResult.Text = "Database Connection Failed!"
            Exit Sub
        End Try

        'Run the Query
        Dim myReader As SqlDataReader = myCommand.ExecuteReader()

        'Set the datasource and bind the fields
        DDLIssue.DataSource = myReader
        DDLIssue.DataTextField = "Issue"
        DDLIssue.DataValueField = "Issue"
        DDLIssue.DataBind()



        'Close the Reader
        myReader.Close()

        'Always close the connection when you are finished
        myConnection.Close()


    End Sub
    Protected Sub DDLIssue_DataBound(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DDLIssue.DataBound
        Dim myListItem As New ListItem()
        myListItem.Text = "Please select..."
        myListItem.Value = "0"
        DDLIssue.Items.Insert(0, myListItem)
    End Sub

My Drop Down List populates with the coding above when I run my application.

I am trying to use this Drop Down List box to load a textbox which I have defined as txtIssue.Text.

I want the user to select an Item from the DDLIssue Drop Down Box and insert the data to the txtIssue textbox when I have the use select an UPDATE button which I have deined in VB code.

The Problem I am having is when I select an Item from the DDLIssue Drop Down it is inserting in the txtIssue textbox as "0".  I need to be able to select an item from the DDLIssue drop down list and have that item appear in the txtIssue Textbox.  I do not want the Index to show which.


I am using the wroing name somehow when I a referencing the DDLIssue drop down list box.  I tried referencing it as DDLIssue.Selectedvalue.text, but I am still seeing "0" here when I debug and hover the mouse over the field with a breakpoint I have set.

Any help is appreciated.  I just do not know how to reference what is in the field when it is coming in.  I want the suer to select an item from the DDLIssue and have that item appear in the txtIssue textbox, not "0"

jjc9809

Answer : Referring to Data in a Drop Down List that is selected

When you are creating the drop down you are  not setting the value member for the id or the text member which will return the correct text string for you.
Random Solutions  
 
programming4us programming4us